summaryrefslogtreecommitdiffstats
path: root/qt/qextscintilla.cpp
blob: 4b33791945c52c893a1a9a47ff01519ac509b3fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
// This module implements the "official" high-level API of the TQt port of
// Scintilla.  It is modelled on TQTextEdit - a method of the same name should
// behave in the same way.
//
// Copyright (c) 2006
// 	Riverbank Computing Limited <info@riverbankcomputing.co.uk>
// 
// This file is part of TQScintilla.
// 
// This copy of TQScintilla 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, or (at your option) any
// later version.
// 
// TQScintilla is supplied 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
// TQScintilla; see the file LICENSE.  If not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.


#include <string.h>
#include <tqapplication.h>
#include <tqcolor.h>

#include "qextscintilla.h"
#include "qextscintillalexer.h"
#include "qextscintillaapis.h"
#include "qextscintillacommandset.h"


// Make sure these match the values in Scintilla.h.  We don't #include that
// file because it just causes more clashes.
#define KEYWORDSET_MAX  8
#define MARKER_MAX  31


// The default fold margin width.
static const int defaultFoldMarginWidth = 14;

// The default set of characters that make up a word.
static const char *defaultWordChars = "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPTQRSTUVWXYZ0123456789";


// The ctor.
QextScintilla::QextScintilla(TQWidget *parent,const char *name,WFlags f)
    : QextScintillaBase(parent,name,f), allocatedMarkers(0), oldPos(-1),
      selText(FALSE), fold(NoFoldStyle), autoInd(FALSE),
      braceMode(NoBraceMatch), acSource(AcsDocument), acThresh(-1),
      acStart(""), acAPIs(0), ctAPIs(0), maxCallTips(-1),
      showSingle(FALSE), modified(FALSE), explicit_fillups(FALSE),
      fillups_enabled(FALSE), saved_fillups("")
{
    connect(this,TQT_SIGNAL(SCN_MODIFYATTEMPTRO()),
             TQT_SIGNAL(modificationAttempted()));

    connect(this,TQT_SIGNAL(SCN_MODIFIED(int,int,const char *,int,int,int,int,int)),
             TQT_SLOT(handleModified(int,int,const char *,int,int,int,int,int)));
    connect(this,TQT_SIGNAL(SCN_CALLTIPCLICK(int)),
             TQT_SLOT(handleCallTipClick(int)));
    connect(this,TQT_SIGNAL(SCN_CHARADDED(int)),
             TQT_SLOT(handleCharAdded(int)));
    connect(this,TQT_SIGNAL(SCN_MARGINCLICK(int,int,int)),
             TQT_SLOT(handleMarginClick(int,int,int)));
    connect(this,TQT_SIGNAL(SCN_SAVEPOINTREACHED()),
             TQT_SLOT(handleSavePointReached()));
    connect(this,TQT_SIGNAL(SCN_SAVEPOINTLEFT()),
             TQT_SLOT(handleSavePointLeft()));
    connect(this,TQT_SIGNAL(SCN_UPDATEUI()),
             TQT_SLOT(handleUpdateUI()));
    connect(this,TQT_SIGNAL(TQSCN_SELCHANGED(bool)),
             TQT_SLOT(handleSelectionChanged(bool)));
    connect(this,TQT_SIGNAL(SCN_USERLISTSELECTION(const char *,int)),
             TQT_SLOT(handleUserListSelection(const char *,int)));

    // Set the default font.
    setFont(TQApplication::font());

    // Set the default fore and background colours.
    TQColorGroup cg = TQApplication::palette().active();
    setColor(cg.text());
    setPaper(cg.base());

#if defined(Q_OS_WIN)
    setEolMode(EolWindows);
#elif defined(Q_OS_MAC)
    setEolMode(EolMac);
#else
    setEolMode(EolUnix);
#endif

    // Capturing the mouse seems to cause problems on multi-head systems.
    // TQt should do the right thing anyway.
    SendScintilla(SCI_SETMOUSEDOWNCAPTURES,0UL);

    SendScintilla(SCI_SETPROPERTY,"fold","1");

    setMatchedBraceForegroundColor(blue);
    setUnmatchedBraceForegroundColor(red);

    setLexer();

    // Set the visible policy.  These are the same as SciTE's defaults
    // which, presumably, are sensible.
    SendScintilla(SCI_SETVISIBLEPOLICY,VISIBLE_STRICT|VISIBLE_SLOP,4);

    // Create the standard command set.
    stdCmds = new QextScintillaCommandSet(this);

    doc.display(this,0);
}


// The dtor.
QextScintilla::~QextScintilla()
{
    doc.undisplay(this);
    delete stdCmds;
}


// Return the current text colour.
TQColor QextScintilla::color() const
{
    return nl_text_colour;
}


// Set the text colour.
void QextScintilla::setColor(const TQColor &c)
{
    if (lex.isNull())
    {
        // Assume style 0 applies to everything so that we don't need to use
        // SCI_STYLECLEARALL which clears everything.
        SendScintilla(SCI_STYLESETFORE, 0, c);
        nl_text_colour = c;
    }
}


// Return the current paper colour.
TQColor QextScintilla::paper() const
{
    return nl_paper_colour;
}


// Set the paper colour.
void QextScintilla::setPaper(const TQColor &c)
{
    if (lex.isNull())
    {
        // Assume style 0 applies to everything so that we don't need to use
        // SCI_STYLECLEARALL which clears everything.  We still have to set the
        // default style as well for the background without any text.
        SendScintilla(SCI_STYLESETBACK, 0, c);
        SendScintilla(SCI_STYLESETBACK, STYLE_DEFAULT, c);
        nl_paper_colour = c;
    }
}


// Set the default font.
void QextScintilla::setFont(const TQFont &f)
{
    if (lex.isNull())
    {
        // Assume style 0 applies to everything so that we don't need to use
        // SCI_STYLECLEARALL which clears everything.
        setStylesFont(f, 0);
        nl_font = f;
    }
}


// Enable/disable auto-indent.
void QextScintilla::setAutoIndent(bool autoindent)
{
    autoInd = autoindent;
}


// Set the brace matching mode.
void QextScintilla::setBraceMatching(BraceMatch bm)
{
    braceMode = bm;
}


// Handle the addition of a character.
void QextScintilla::handleCharAdded(int ch)
{
    // Ignore if there is a selection.
    long pos = SendScintilla(SCI_GETSELECTIONSTART);

    if (pos != SendScintilla(SCI_GETSELECTIONEND) || pos == 0)
        return;

    // If auto-completion is already active then see if this character is a
    // start character.  If it is then create a new list which will be a
    // subset of the current one.  The case where it isn't a start
    // character seem to be handled correctly elsewhere.
    if (isListActive())
    {
        if (isAutoCStartChar(ch))
        {
            cancelList();
            startAutoCompletion(acSource, FALSE, FALSE);
        }

        return;
    }

    // Handle call tips.
    if (strchr("(),", ch) != NULL)
        callTip();

    // Handle auto-indentation.
    if (autoInd)
        if (lex.isNull() || (lex -> autoIndentStyle() & AiMaintain))
            maintainIndentation(ch,pos);
        else
            autoIndentation(ch,pos);

    // See if we might want to start auto-completion.
    if (!isCallTipActive())
        if (isAutoCStartChar(ch))
            startAutoCompletion(acSource, FALSE, FALSE);
        else if (acThresh >= 1 && isWordChar(ch))
            startAutoCompletion(acSource, TRUE, FALSE);
}


// See if a call tip is active.
bool QextScintilla::isCallTipActive()
{
    return SendScintilla(SCI_CALLTIPACTIVE);
}


// Handle a possible change to any current call tip.
void QextScintilla::callTip()
{
    if (!ctAPIs)
        return;

    long pos = SendScintilla(SCI_GETCURRENTPOS);
    long linenr = SendScintilla(SCI_LINEFROMPOSITION,pos);
    long linelen = SendScintilla(SCI_LINELENGTH,linenr);

    char *lbuf = new char[linelen + 1];

    int loff = SendScintilla(SCI_GETCURLINE,linelen + 1,lbuf);

    int commas = 0, start = -1;

    // Move backwards through the line looking for the start of the current
    // call tip and working out which argument it is.
    while (loff > 0)
    {
        char ch = lbuf[--loff];

        if (ch == ',')
            ++commas;
        else if (ch == ')')
        {
            int depth = 1;

            // Ignore everything back to the start of the
            // corresponding parenthesis.
            while (loff > 0)
            {
                ch = lbuf[--loff];

                if (ch == ')')
                    ++depth;
                else if (ch == '(' && --depth == 0)
                    break;
            }
        }
        else if (ch == '(' && loff > 0)
        {
            if (isWordChar(lbuf[loff - 1]))
            {
                // The parenthesis is preceded by a word so
                // find the start of that word.
                lbuf[loff--] = '\0';

                while (loff >= 0)
                {
                    if (!isWordChar(lbuf[loff]) && !isAutoCStartChar(lbuf[loff]))
                        break;

                    --loff;
                }

                start = loff + 1;
                break;
            }

            // We are between parentheses that do not correspond to
            // a call tip, so reset the argument count.
            commas = 0;
        }
    }

    // Cancel any existing call tip.
    SendScintilla(SCI_CALLTIPCANCEL);

    // Done if there is no new call tip to set.
    if (start < 0)
    {
        delete []lbuf;
        return;
    }

    TQString ct = ctAPIs -> callTips(&lbuf[start],maxCallTips,commas);

    delete []lbuf;

    if (ct.isEmpty())
        return;

    ctpos = SendScintilla(SCI_POSITIONFROMLINE,linenr) + start;

    SendScintilla(SCI_CALLTIPSHOW,ctpos,ct.latin1());

    // Done if there is more than one line in the call tip or there isn't a
    // down arrow at the start.
    if (ct[0] == '\002' || ct.find('\n') >= 0)
        return;

    // Highlight the current argument.
    int astart;

    if (commas == 0)
        astart = ct.find('(');
    else
    {
        astart = -1;

        do
            astart = ct.find(',',astart + 1);
        while (astart >= 0 && --commas > 0);
    }

    int len = ct.length();

    if (astart < 0 || ++astart == len)
        return;

    // The end is at the next comma or unmatched closing parenthesis.
    int aend, depth = 0;

    for (aend = astart; aend < len; ++aend)
    {
        TQChar ch = ct.at(aend);

        if (ch == ',' && depth == 0)
            break;
        else if (ch == '(')
            ++depth;
        else if (ch == ')')
        {
            if (depth == 0)
                break;

            --depth;
        }
    }

    if (astart != aend)
        SendScintilla(SCI_CALLTIPSETHLT,astart,aend);
}


// Handle a call tip click.
void QextScintilla::handleCallTipClick(int dir)
{
    if (!ctAPIs)
        return;

    TQString ct = ctAPIs -> callTipsNextPrev(dir);

    if (ct.isNull())
        return;

    SendScintilla(SCI_CALLTIPSHOW,ctpos,ct.latin1());
}


// Possibly start auto-completion.
void QextScintilla::startAutoCompletion(AutoCompletionSource acs,
        bool checkThresh, bool single)
{
    // Get the current line.
    long len = SendScintilla(SCI_GETCURLINE) + 1;

    char *line = new char[len];

    int wend = SendScintilla(SCI_GETCURLINE, len, line);

    // Find the start of the auto-completion text.
    int wstart = wend;
    bool numeric = true;

    while (wstart > 0)
    {
        char ch = line[wstart - 1];

        // Don't auto-complete numbers.
        if (ch < '0' || ch > '9')
            numeric = false;

        if (!isWordChar(ch) && !isAutoCStartChar(ch))
            break;

        --wstart;
    }

    int wlen = wend - wstart;

    if (numeric || wlen == 0 || (checkThresh && wlen < acThresh))
        return;

    // Isolate the auto-completion text.
    char *word = &line[wstart];
    line[wend] = '\0';

    // Generate the string representing the valid words to select from.
    TQStringList wlist;
    bool cs = !SendScintilla(SCI_AUTOCGETIGNORECASE);

    if (acs == AcsAll || acs == AcsDocument)
    {
        SendScintilla(SCI_SETSEARCHFLAGS,SCFIND_WORDSTART | (cs ? SCFIND_MATCHCASE : 0));

        long pos = 0;
        long dlen = SendScintilla(SCI_GETLENGTH);
        long caret = SendScintilla(SCI_GETCURRENTPOS);
        TQString root(word);

        for (;;)
        {
            long fstart;

            SendScintilla(SCI_SETTARGETSTART,pos);
            SendScintilla(SCI_SETTARGETEND,dlen);

            if ((fstart = SendScintilla(SCI_SEARCHINTARGET,wlen,word)) < 0)
                break;

            // Move past the root part.
            pos = fstart + wlen;

            // Skip if this is the word we are auto-completing.
            if (pos == caret)
                continue;

            // Get the rest of this word.
            TQString w(root);

            while (pos < dlen)
            {
                char ch = SendScintilla(SCI_GETCHARAT,pos);

                if (!isWordChar(ch))
                    break;

                w += ch;

                ++pos;
            }

            // Add the word if it isn't already there.
            if (wlist.findIndex(w) < 0)
                wlist.append(w);
        }
    }

    if ((acs == AcsAll || acs == AcsAPIs) && acAPIs)
        acAPIs->autoCompletionList(word, cs, wlist);

    delete []line;

    if (wlist.isEmpty())
        return;

    wlist.sort();

    const char sep = '\x03';

    SendScintilla(SCI_AUTOCSETCHOOSESINGLE,single);
    SendScintilla(SCI_AUTOCSETSEPARATOR, sep);
    SendScintilla(SCI_AUTOCSHOW, wlen, wlist.join(TQChar(sep)).latin1());
}


// Check if a character is an auto-completion start character.
bool QextScintilla::isAutoCStartChar(char ch) const
{
    const char *start_chars = 0;

    if (!lex.isNull())
        start_chars = lex->autoCompletionStartCharacters();

    if (!start_chars)
        start_chars = acStart;

    return (strchr(start_chars, ch) != NULL);
}


// Maintain the indentation of the previous line.
void QextScintilla::maintainIndentation(char ch,long pos)
{
    if (ch != '\r' && ch != '\n')
        return;

    int curr_line = SendScintilla(SCI_LINEFROMPOSITION,pos);

    // Get the indentation of the preceding non-zero length line.
    int ind = 0;

    for (int line = curr_line - 1; line >= 0; --line)
    {
        if (SendScintilla(SCI_GETLINEENDPOSITION,line) >
            SendScintilla(SCI_POSITIONFROMLINE,line))
        {
            ind = indentation(line);
            break;
        }
    }

    if (ind > 0)
        autoIndentLine(pos,curr_line,ind);
}


// Implement auto-indentation.
void QextScintilla::autoIndentation(char ch,long pos)
{
    int curr_line = SendScintilla(SCI_LINEFROMPOSITION,pos);
    int ind_width = indentationWidth();
    long curr_line_start = SendScintilla(SCI_POSITIONFROMLINE,curr_line);

    const char *block_start = lex -> blockStart();
    bool start_single = (block_start && strlen(block_start) == 1);

    const char *block_end = lex -> blockEnd();
    bool end_single = (block_end && strlen(block_end) == 1);

    if (end_single && block_end[0] == ch)
    {
        if ((lex -> autoIndentStyle() & AiClosing) && rangeIsWhitespace(curr_line_start,pos - 1))
            autoIndentLine(pos,curr_line,blockIndent(curr_line - 1) - indentationWidth());
    }
    else if (start_single && block_start[0] == ch)
    {
        // De-indent if we have already indented because the previous
        // line was a start of block keyword.
        if ((lex->autoIndentStyle() & AiOpening) && curr_line > 0 && getIndentState(curr_line - 1) == isKeywordStart && rangeIsWhitespace(curr_line_start, pos - 1))
            autoIndentLine(pos,curr_line,blockIndent(curr_line - 1) - indentationWidth());
    }
    else if (ch == '\r' || ch == '\n')
        autoIndentLine(pos,curr_line,blockIndent(curr_line - 1));
}


// Set the indentation for a line.
void QextScintilla::autoIndentLine(long pos,int line,int indent)
{
    if (indent < 0)
        return;

    long pos_before = SendScintilla(SCI_GETLINEINDENTPOSITION,line);
    SendScintilla(SCI_SETLINEINDENTATION,line,indent);
    long pos_after = SendScintilla(SCI_GETLINEINDENTPOSITION,line);
    long new_pos = -1;

    if (pos_after > pos_before)
        new_pos = pos + (pos_after - pos_before);
    else if (pos_after < pos_before && pos >= pos_after)
        if (pos >= pos_before)
            new_pos = pos + (pos_after - pos_before);
        else
            new_pos = pos_after;

    if (new_pos >= 0)
        SendScintilla(SCI_SETSEL,new_pos,new_pos);
}


// Return the indentation of the block defined by the given line (or something
// significant before).
int QextScintilla::blockIndent(int line)
{
    if (line < 0)
        return 0;

    // Handle the trvial case.
    if (!lex -> blockStartKeyword() && !lex -> blockStart() && !lex -> blockEnd())
        return indentation(line);

    int line_limit = line - lex -> blockLookback();

    if (line_limit < 0)
        line_limit = 0;

    for (int l = line; l >= line_limit; --l)
    {
        IndentState istate = getIndentState(l);

        if (istate != isNone)
        {
            int ind_width = indentationWidth();
            int ind = indentation(l);

            if (istate == isBlockStart)
            {
                if (lex -> autoIndentStyle() & AiOpening)
                    ind += ind_width;
            }
            else if (istate == isBlockEnd)
            {
                if (!(lex -> autoIndentStyle() & AiClosing))
                    ind -= ind_width;

                if (ind < 0)
                    ind = 0;
            }
            else if (line == l)
                ind += ind_width;

            return ind;
        }
    }

    return indentation(line);
}


// Return TRUE if all characters starting at spos up to, but not including
// epos, are spaces or tabs.
bool QextScintilla::rangeIsWhitespace(long spos,long epos)
{
    while (spos < epos)
    {
        char ch = SendScintilla(SCI_GETCHARAT,spos);

        if (ch != ' ' && ch != '\t')
            return FALSE;

        ++spos;
    }

    return TRUE;
}


// Returns the indentation state of a line.
QextScintilla::IndentState QextScintilla::getIndentState(int line)
{
    IndentState istate;

    // Get the styled text.
    long spos = SendScintilla(SCI_POSITIONFROMLINE,line);
    long epos = SendScintilla(SCI_POSITIONFROMLINE,line + 1);

    char *text = new char[(epos - spos + 1) * 2];

    SendScintilla(SCI_GETSTYLEDTEXT,spos,epos,text);

    int style, bstart_off, bend_off;

    // Block start/end takes precedence over keywords.
    const char *bstart_words = lex->blockStart(&style);
    bstart_off = findStyledWord(text, style, bstart_words);

    const char *bend_words = lex->blockEnd(&style);
    bend_off = findStyledWord(text, style, bend_words);

    // If there is a block start but no block end characters then ignore it
    // unless the block start is the last significant thing on the line,
    // ie. assume Python-like blocking.
    if (bstart_off >= 0 && !bend_words)
        for (int i = bstart_off * 2; text[i] != '\0'; i += 2)
            if (!TQChar(text[i]).isSpace())
                return isNone;

    if (bstart_off > bend_off)
        istate = isBlockStart;
    else if (bend_off > bstart_off)
        istate = isBlockEnd;
    else
    {
        const char *words = lex->blockStartKeyword(&style);

        istate = (findStyledWord(text,style,words) >= 0) ? isKeywordStart : isNone;
    }

    delete[] text;

    return istate;
}


// text is a pointer to some styled text (ie. a character byte followed by a
// style byte).  style is a style number.  words is a space separated list of
// words.  Returns the position in the text immediately after the last one of
// the words with the style.  The reason we are after the last, and not the
// first, occurance is that we are looking for words that start and end a block
// where the latest one is the most significant.
int QextScintilla::findStyledWord(const char *text,int style,const char *words)
{
    if (!words)
        return -1;

    // Find the range of text with the style we are looking for.
    const char *stext;

    for (stext = text; stext[1] != style; stext += 2)
        if (stext[0] == '\0')
            return -1;

    // Move to the last character.
    const char *etext = stext;

    while (etext[2] != '\0')
        etext += 2;

    // Backtrack until we find the style.  There will be one.
    while (etext[1] != style)
        etext -= 2;

    // Look for each word in turn.
    while (words[0] != '\0')
    {
        // Find the end of the word.
        const char *eword = words;

        while (eword[1] != ' ' && eword[1] != '\0')
            ++eword;

        // Now search the text backwards.
        const char *wp = eword;

        for (const char *tp = etext; tp >= stext; tp -= 2)
        {
            if (tp[0] != wp[0] || tp[1] != style)
            {
                // Reset the search.
                wp = eword;
                continue;
            }

            // See if all the word has matched.
            if (wp-- == words)
                return ((tp - text) / 2) + (eword - words) + 1;
        }

        // Move to the start of the next word if there is one.
        words = eword + 1;

        if (words[0] == ' ')
            ++words;
    }

    return -1;
}


// Return TRUE if the code page is UTF8.
bool QextScintilla::isUtf8()
{
    return (SendScintilla(SCI_GETCODEPAGE) == SC_CP_UTF8);
}


// Set the code page.
void QextScintilla::setUtf8(bool cp)
{
    SendScintilla(SCI_SETCODEPAGE,(cp ? SC_CP_UTF8 : 0));
}


// Return the end-of-line mode.
QextScintilla::EolMode QextScintilla::eolMode()
{
    return (EolMode)SendScintilla(SCI_GETEOLMODE);
}


// Set the end-of-line mode.
void QextScintilla::setEolMode(EolMode mode)
{
    SendScintilla(SCI_SETEOLMODE,mode);
}


// Convert the end-of-lines to a particular mode.
void QextScintilla::convertEols(EolMode mode)
{
    SendScintilla(SCI_CONVERTEOLS,mode);
}


// Return the edge colour.
TQColor QextScintilla::edgeColor()
{
        long res = SendScintilla(SCI_GETEDGECOLOUR);

        return TQColor((int)res, ((int)(res >> 8)) & 0x00ff, ((int)(res >> 16)) & 0x00ff);
}


// Set the edge colour.
void QextScintilla::setEdgeColor(const TQColor &col)
{
    SendScintilla(SCI_SETEDGECOLOUR,col);
}


// Return the edge column.
int QextScintilla::edgeColumn()
{
    return SendScintilla(SCI_GETEDGECOLUMN);
}


// Set the edge column.
void QextScintilla::setEdgeColumn(int colnr)
{
    SendScintilla(SCI_SETEDGECOLUMN,colnr);
}


// Return the edge mode.
QextScintilla::EdgeMode QextScintilla::edgeMode()
{
    return (EdgeMode)SendScintilla(SCI_GETEDGEMODE);
}


// Set the edge mode.
void QextScintilla::setEdgeMode(EdgeMode mode)
{
    SendScintilla(SCI_SETEDGEMODE,mode);
}


// Return the end-of-line visibility.
bool QextScintilla::eolVisibility()
{
    return SendScintilla(SCI_GETVIEWEOL);
}


// Set the end-of-line visibility.
void QextScintilla::setEolVisibility(bool visible)
{
    SendScintilla(SCI_SETVIEWEOL,visible);
}


// Return the whitespace visibility.
QextScintilla::WhitespaceVisibility QextScintilla::whitespaceVisibility()
{
    return (WhitespaceVisibility)SendScintilla(SCI_GETVIEWWS);
}


// Set the whitespace visibility.
void QextScintilla::setWhitespaceVisibility(WhitespaceVisibility mode)
{
    SendScintilla(SCI_SETVIEWWS,mode);
}


// Return the line wrap mode.
QextScintilla::WrapMode QextScintilla::wrapMode()
{
    return (WrapMode)SendScintilla(SCI_GETWRAPMODE);
}


// Set the line wrap mode.
void QextScintilla::setWrapMode(WrapMode mode)
{
    SendScintilla(SCI_SETLAYOUTCACHE, (mode == WrapNone ? SC_CACHE_CARET : SC_CACHE_DOCUMENT));
    SendScintilla(SCI_SETWRAPMODE, mode);
}


// Set the line wrap visual flags.
void QextScintilla::setWrapVisualFlags(WrapVisualFlag eflag,
        WrapVisualFlag sflag, int sindent)
{
    int flags = SC_WRAPVISUALFLAG_NONE;
    int loc = SC_WRAPVISUALFLAGLOC_DEFAULT;

    if (eflag == WrapFlagByText)
    {
        flags |= SC_WRAPVISUALFLAG_END;
        loc |= SC_WRAPVISUALFLAGLOC_END_BY_TEXT;
    }
    else if (eflag == WrapFlagByBorder)
        flags |= SC_WRAPVISUALFLAG_END;

    if (sflag == WrapFlagByText)
    {
        flags |= SC_WRAPVISUALFLAG_START;
        loc |= SC_WRAPVISUALFLAGLOC_START_BY_TEXT;
    }
    else if (sflag == WrapFlagByBorder)
        flags |= SC_WRAPVISUALFLAG_START;

    SendScintilla(SCI_SETWRAPVISUALFLAGS, flags);
    SendScintilla(SCI_SETWRAPVISUALFLAGSLOCATION, loc);
    SendScintilla(SCI_SETWRAPSTARTINDENT, sindent);
}


// Set the folding style.
void QextScintilla::setFolding(FoldStyle folding)
{
    fold = folding;

    if (folding == NoFoldStyle)
    {
        SendScintilla(SCI_SETMARGINWIDTHN,2,0L);
        return;
    }

    int mask = SendScintilla(SCI_GETMODEVENTMASK);
    SendScintilla(SCI_SETMODEVENTMASK,mask | SC_MOD_CHANGEFOLD);

    SendScintilla(SCI_SETFOLDFLAGS,SC_FOLDFLAG_LINEAFTER_CONTRACTED);

    SendScintilla(SCI_SETMARGINTYPEN,2,(long)SC_MARGIN_SYMBOL);
    SendScintilla(SCI_SETMARGINMASKN,2,SC_MASK_FOLDERS);
    SendScintilla(SCI_SETMARGINSENSITIVEN,2,1);

    // Set the marker symbols to use.
    switch (folding)
    {
    case PlainFoldStyle:
        setFoldMarker(SC_MARKNUM_FOLDEROPEN,SC_MARK_MINUS);
        setFoldMarker(SC_MARKNUM_FOLDER,SC_MARK_PLUS);
        setFoldMarker(SC_MARKNUM_FOLDERSUB);
        setFoldMarker(SC_MARKNUM_FOLDERTAIL);
        setFoldMarker(SC_MARKNUM_FOLDEREND);
        setFoldMarker(SC_MARKNUM_FOLDEROPENMID);
        setFoldMarker(SC_MARKNUM_FOLDERMIDTAIL);

        break;

    case CircledFoldStyle:
        setFoldMarker(SC_MARKNUM_FOLDEROPEN,SC_MARK_CIRCLEMINUS);
        setFoldMarker(SC_MARKNUM_FOLDER,SC_MARK_CIRCLEPLUS);
        setFoldMarker(SC_MARKNUM_FOLDERSUB);
        setFoldMarker(SC_MARKNUM_FOLDERTAIL);
        setFoldMarker(SC_MARKNUM_FOLDEREND);
        setFoldMarker(SC_MARKNUM_FOLDEROPENMID);
        setFoldMarker(SC_MARKNUM_FOLDERMIDTAIL);

        break;

    case BoxedFoldStyle:
        setFoldMarker(SC_MARKNUM_FOLDEROPEN,SC_MARK_BOXMINUS);
        setFoldMarker(SC_MARKNUM_FOLDER,SC_MARK_BOXPLUS);
        setFoldMarker(SC_MARKNUM_FOLDERSUB);
        setFoldMarker(SC_MARKNUM_FOLDERTAIL);
        setFoldMarker(SC_MARKNUM_FOLDEREND);
        setFoldMarker(SC_MARKNUM_FOLDEROPENMID);
        setFoldMarker(SC_MARKNUM_FOLDERMIDTAIL);

        break;

    case CircledTreeFoldStyle:
        setFoldMarker(SC_MARKNUM_FOLDEROPEN,SC_MARK_CIRCLEMINUS);
        setFoldMarker(SC_MARKNUM_FOLDER,SC_MARK_CIRCLEPLUS);
        setFoldMarker(SC_MARKNUM_FOLDERSUB,SC_MARK_VLINE);
        setFoldMarker(SC_MARKNUM_FOLDERTAIL,SC_MARK_LCORNERCURVE);
        setFoldMarker(SC_MARKNUM_FOLDEREND,SC_MARK_CIRCLEPLUSCONNECTED);
        setFoldMarker(SC_MARKNUM_FOLDEROPENMID,SC_MARK_CIRCLEMINUSCONNECTED);
        setFoldMarker(SC_MARKNUM_FOLDERMIDTAIL,SC_MARK_TCORNERCURVE);

        break;

    case BoxedTreeFoldStyle:
        setFoldMarker(SC_MARKNUM_FOLDEROPEN,SC_MARK_BOXMINUS);
        setFoldMarker(SC_MARKNUM_FOLDER,SC_MARK_BOXPLUS);
        setFoldMarker(SC_MARKNUM_FOLDERSUB,SC_MARK_VLINE);
        setFoldMarker(SC_MARKNUM_FOLDERTAIL,SC_MARK_LCORNER);
        setFoldMarker(SC_MARKNUM_FOLDEREND,SC_MARK_BOXPLUSCONNECTED);
        setFoldMarker(SC_MARKNUM_FOLDEROPENMID,SC_MARK_BOXMINUSCONNECTED);
        setFoldMarker(SC_MARKNUM_FOLDERMIDTAIL,SC_MARK_TCORNER);

        break;
    }

    SendScintilla(SCI_SETMARGINWIDTHN,2,defaultFoldMarginWidth);
}


// Set up a folder marker.
void QextScintilla::setFoldMarker(int marknr,int mark)
{
    SendScintilla(SCI_MARKERDEFINE,marknr,mark);

    if (mark != SC_MARK_EMPTY)
    {
        SendScintilla(SCI_MARKERSETFORE,marknr,white);
        SendScintilla(SCI_MARKERSETBACK,marknr,black);
    }
}


// Handle a click in the fold margin.  This is mostly taken from SciTE.
void QextScintilla::foldClick(int lineClick,int bstate)
{
    if ((bstate & ShiftButton) && (bstate & ControlButton))
    {
        foldAll();
        return;
    }

    int levelClick = SendScintilla(SCI_GETFOLDLEVEL,lineClick);

    if (levelClick & SC_FOLDLEVELHEADERFLAG)
    {
        if (bstate & ShiftButton)
        {
            // Ensure all tqchildren are visible.
            SendScintilla(SCI_SETFOLDEXPANDED,lineClick,1);
            foldExpand(lineClick,TRUE,TRUE,100,levelClick);
        }
        else if (bstate & ControlButton)
        {
            if (SendScintilla(SCI_GETFOLDEXPANDED,lineClick))
            {
                // Contract this line and all its tqchildren.
                SendScintilla(SCI_SETFOLDEXPANDED,lineClick,0L);
                foldExpand(lineClick,FALSE,TRUE,0,levelClick);
            }
            else
            {
                // Expand this line and all its tqchildren.
                SendScintilla(SCI_SETFOLDEXPANDED,lineClick,1);
                foldExpand(lineClick,TRUE,TRUE,100,levelClick);
            }
        }
        else
        {
            // Toggle this line.
            SendScintilla(SCI_TOGGLEFOLD,lineClick);
        }
    }
}


// Do the hard work of hiding and showing lines.  This is mostly taken from
// SciTE.
void QextScintilla::foldExpand(int &line,bool doExpand,bool force,
                   int visLevels,int level)
{
    int lineMaxSubord = SendScintilla(SCI_GETLASTCHILD,line,level & SC_FOLDLEVELNUMBERMASK);

    line++;

    while (line <= lineMaxSubord)
    {
        if (force)
        {
            if (visLevels > 0)
                SendScintilla(SCI_SHOWLINES,line,line);
            else
                SendScintilla(SCI_HIDELINES,line,line);
        }
        else if (doExpand)
            SendScintilla(SCI_SHOWLINES,line,line);

        int levelLine = level;

        if (levelLine == -1)
            levelLine = SendScintilla(SCI_GETFOLDLEVEL,line);

        if (levelLine & SC_FOLDLEVELHEADERFLAG)
        {
            if (force)
            {
                if (visLevels > 1)
                    SendScintilla(SCI_SETFOLDEXPANDED,line,1);
                else
                    SendScintilla(SCI_SETFOLDEXPANDED,line,0L);

                foldExpand(line,doExpand,force,visLevels - 1);
            }
            else if (doExpand)
            {
                if (!SendScintilla(SCI_GETFOLDEXPANDED,line))
                    SendScintilla(SCI_SETFOLDEXPANDED,line,1);

                foldExpand(line,TRUE,force,visLevels - 1);
            }
            else
                foldExpand(line,FALSE,force,visLevels - 1);
        }
        else
            line++;
    }
}


// Fully expand (if there is any line currently folded) all text.  Otherwise,
// fold all text.  This is mostly taken from SciTE.
void QextScintilla::foldAll(bool tqchildren)
{
    recolor();

    int maxLine = SendScintilla(SCI_GETLINECOUNT);
    bool expanding = TRUE;

    for (int lineSeek = 0; lineSeek < maxLine; lineSeek++)
    {
        if (SendScintilla(SCI_GETFOLDLEVEL,lineSeek) & SC_FOLDLEVELHEADERFLAG)
        {
            expanding = !SendScintilla(SCI_GETFOLDEXPANDED,lineSeek);
            break;
        }
    }

    for (int line = 0; line < maxLine; line++)
    {
        int level = SendScintilla(SCI_GETFOLDLEVEL,line);

        if (!(level & SC_FOLDLEVELHEADERFLAG))
            continue;

        if (tqchildren ||
            (SC_FOLDLEVELBASE == (level & SC_FOLDLEVELNUMBERMASK)))
        {
            if (expanding)
            {
                SendScintilla(SCI_SETFOLDEXPANDED,line,1);
                foldExpand(line,TRUE,FALSE,0,level);
                line--;
            }
            else
            {
                int lineMaxSubord = SendScintilla(SCI_GETLASTCHILD,line,-1);

                SendScintilla(SCI_SETFOLDEXPANDED,line,0L);

                if (lineMaxSubord > line)
                    SendScintilla(SCI_HIDELINES,line + 1,lineMaxSubord);
            }
        }
    }
}


// Handle a fold change.  This is mostly taken from SciTE.
void QextScintilla::foldChanged(int line,int levelNow,int levelPrev)
{
        if (levelNow & SC_FOLDLEVELHEADERFLAG)
    {
                if (!(levelPrev & SC_FOLDLEVELHEADERFLAG))
                        SendScintilla(SCI_SETFOLDEXPANDED,line,1);
        }
    else if (levelPrev & SC_FOLDLEVELHEADERFLAG)
    {
                if (!SendScintilla(SCI_GETFOLDEXPANDED,line))
        {
                        // Removing the fold from one that has been contracted
            // so should expand.  Otherwise lines are left
            // invisible with no way to make them visible.
                        foldExpand(line,TRUE,FALSE,0,levelPrev);
                }
        }
}


// Toggle the fold for a line if it contains a fold marker.
void QextScintilla::foldLine(int line)
{
    SendScintilla(SCI_TOGGLEFOLD,line);
}


// Handle the SCN_MODIFIED notification.
void QextScintilla::handleModified(int pos,int mtype,const char *text,int len,
                   int added,int line,int foldNow,int foldPrev)
{
    if (mtype & SC_MOD_CHANGEFOLD)
    {
        if (fold)
            foldChanged(line,foldNow,foldPrev);
    }
    else if (mtype & (SC_MOD_INSERTTEXT | SC_MOD_DELETETEXT))
        emit textChanged();
}


// Zoom in a number of points.
void QextScintilla::zoomIn(int range)
{
    zoomTo(SendScintilla(SCI_GETZOOM) + range);
}


// Zoom in a single point.
void QextScintilla::zoomIn()
{
    SendScintilla(SCI_ZOOMIN);
}


// Zoom out a number of points.
void QextScintilla::zoomOut(int range)
{
    zoomTo(SendScintilla(SCI_GETZOOM) - range);
}


// Zoom out a single point.
void QextScintilla::zoomOut()
{
    SendScintilla(SCI_ZOOMOUT);
}


// Set the zoom to a number of points.
void QextScintilla::zoomTo(int size)
{
    if (size < -10)
        size = -10;
    else if (size > 20)
        size = 20;

    SendScintilla(SCI_SETZOOM,size);
}


// Find the first occurrence of a string.
bool QextScintilla::findFirst(const TQString &expr,bool re,bool cs,bool wo,
                              bool wrap,bool forward,int line,int index,
                  bool show)
{
    findState.inProgress = FALSE;

    if (expr.isEmpty())
        return FALSE;

    findState.expr = expr;
    findState.wrap = wrap;
    findState.forward = forward;

    findState.flags = (cs ? SCFIND_MATCHCASE : 0) |
              (wo ? SCFIND_WHOLEWORD : 0) |
              (re ? SCFIND_REGEXP : 0);

    if (line < 0 || index < 0)
        findState.startpos = SendScintilla(SCI_GETCURRENTPOS);
    else
        findState.startpos = posFromLineIndex(line,index);

    if (forward)
        findState.endpos = SendScintilla(SCI_GETLENGTH);
    else
        findState.endpos = 0;

    findState.show = show;

    return doFind();
}


// Find the next occurrence of a string.
bool QextScintilla::findNext()
{
    if (!findState.inProgress)
        return FALSE;

    return doFind();
}


// Do the hard work of findFirst() and findNext().
bool QextScintilla::doFind()
{
    SendScintilla(SCI_SETSEARCHFLAGS,findState.flags);

    long pos = simpleFind();

    // See if it was found.  If not and wraparound is wanted, try again.
    if (pos == -1 && findState.wrap)
    {
        if (findState.forward)
        {
            findState.startpos = 0;
            findState.endpos = SendScintilla(SCI_GETLENGTH);
        }
        else
        {
            findState.startpos = SendScintilla(SCI_GETLENGTH);
            findState.endpos = 0;
        }

        pos = simpleFind();
    }

    if (pos == -1)
    {
        findState.inProgress = FALSE;
        return FALSE;
    }

    // It was found.
    long targstart = SendScintilla(SCI_GETTARGETSTART);
    long targend = SendScintilla(SCI_GETTARGETEND);

    // Ensure the text found is visible if required.
    if (findState.show)
    {
        int startLine = SendScintilla(SCI_LINEFROMPOSITION,targstart);
        int endLine = SendScintilla(SCI_LINEFROMPOSITION,targend);

        for (int i = startLine; i <= endLine; ++i)
            SendScintilla(SCI_ENSUREVISIBLEENFORCEPOLICY,i);
    }

    // Now set the selection.
    SendScintilla(SCI_SETSEL,targstart,targend);

    // Finally adjust the start position so that we don't find the same one
    // again.
    if (findState.forward)
        findState.startpos = targend;
    else if ((findState.startpos = targstart - 1) < 0)
        findState.startpos = 0;

    findState.inProgress = TRUE;
    return TRUE;
}


// Do a simple find between the start and end positions.
long QextScintilla::simpleFind()
{
    if (findState.startpos == findState.endpos)
        return -1;

    SendScintilla(SCI_SETTARGETSTART,findState.startpos);
    SendScintilla(SCI_SETTARGETEND,findState.endpos);

    long pos;

    if (isUtf8())
    {
        TQCString s = findState.expr.utf8();

        pos = SendScintilla(SCI_SEARCHINTARGET,s.length(),s.data());
    }
    else
    {
        const char *s = findState.expr.latin1();

        pos = SendScintilla(SCI_SEARCHINTARGET,strlen(s),s);
    }

    return pos;
}


// Replace the text found with the previous findFirst() or findNext().
void QextScintilla::replace(const TQString &replaceStr)
{
    if (!findState.inProgress)
        return;

    long start = SendScintilla(SCI_GETSELECTIONSTART);

    SendScintilla(SCI_TARGETFROMSELECTION);

    long len;
    int cmd = (findState.flags & SCFIND_REGEXP) ? SCI_REPLACETARGETRE : SCI_REPLACETARGET;

    if (isUtf8())
        len = SendScintilla(cmd,-1,replaceStr.utf8().data());
    else
        len = SendScintilla(cmd,-1,replaceStr.latin1());

    // Reset the selection.
    SendScintilla(SCI_SETSELECTIONSTART,start);
    SendScintilla(SCI_SETSELECTIONEND,start + len);

    if (findState.forward)
        findState.startpos = start + len;
}


// Query the modified state.
bool QextScintilla::isModified()
{
    // We don't use SCI_GETMODIFY as it seems to be buggy in Scintilla
    // v1.61.
    return modified;
}


// Set the modified state.
void QextScintilla::setModified(bool m)
{
    if (!m)
        SendScintilla(SCI_SETSAVEPOINT);
}


// Handle the SCN_MARGINCLICK notification.
void QextScintilla::handleMarginClick(int pos,int modifiers,int margin)
{
    int state = 0;

    if (modifiers & SCMOD_SHIFT)
        state |= ShiftButton;

    if (modifiers & SCMOD_CTRL)
        state |= ControlButton;

    if (modifiers & SCMOD_ALT)
        state |= AltButton;

    int line = SendScintilla(SCI_LINEFROMPOSITION,pos);

    if (fold && margin == 2)
        foldClick(line,state);
    else
        emit marginClicked(margin,line,(ButtonState)state);
}


// Handle the SCN_SAVEPOINTREACHED notification.
void QextScintilla::handleSavePointReached()
{
    if (modified)
    {
        modified = FALSE;
        emit modificationChanged(FALSE);
    }
}


// Handle the SCN_SAVEPOINTLEFT notification.
void QextScintilla::handleSavePointLeft()
{
    if (!modified)
    {
        modified = TRUE;
        emit modificationChanged(TRUE);
    }
}


// Handle the TQSCN_SELCHANGED signal.
void QextScintilla::handleSelectionChanged(bool yes)
{
    selText = yes;

    emit copyAvailable(yes);
    emit selectionChanged();
}


// Get the current selection.
void QextScintilla::getSelection(int *lineFrom,int *indexFrom,
                 int *lineTo,int *indexTo)
{
    if (selText)
    {
        lineIndexFromPos(SendScintilla(SCI_GETSELECTIONSTART),
                 lineFrom,indexFrom);
        lineIndexFromPos(SendScintilla(SCI_GETSELECTIONEND),
                 lineTo,indexTo);
    }
    else
        *lineFrom = *indexFrom = *lineTo = *indexTo = -1;
}


// Sets the current selection.
void QextScintilla::setSelection(int lineFrom,int indexFrom,
                 int lineTo,int indexTo)
{
    SendScintilla(SCI_SETSELECTIONSTART,posFromLineIndex(lineFrom,indexFrom));
    SendScintilla(SCI_SETSELECTIONEND,posFromLineIndex(lineTo,indexTo));
}


// Set the background colour of selected text.
void QextScintilla::setSelectionBackgroundColor(const TQColor &col)
{
    SendScintilla(SCI_SETSELBACK,1,col);

    int alpha = tqAlpha(col.rgb());
    
    if (alpha < 255)
        SendScintilla(SCI_SETSELALPHA, alpha);
}


// Set the foreground colour of selected text.
void QextScintilla::setSelectionForegroundColor(const TQColor &col)
{
    SendScintilla(SCI_SETSELFORE,1,col);
}


// Reset the background colour of selected text to the default.
void QextScintilla::resetSelectionBackgroundColor()
{
    SendScintilla(SCI_SETSELALPHA, SC_ALPHA_NOALPHA);
    SendScintilla(SCI_SETSELBACK,0UL);
}


// Reset the foreground colour of selected text to the default.
void QextScintilla::resetSelectionForegroundColor()
{
    SendScintilla(SCI_SETSELFORE,0UL);
}


// Set the width of the caret.
void QextScintilla::setCaretWidth(int width)
{
    SendScintilla(SCI_SETCARETWIDTH,width);
}


// Set the foreground colour of the caret.
void QextScintilla::setCaretForegroundColor(const TQColor &col)
{
    SendScintilla(SCI_SETCARETFORE,col);
}


// Set the background colour of the line containing the caret.
void QextScintilla::setCaretLineBackgroundColor(const TQColor &col)
{
    SendScintilla(SCI_SETCARETLINEBACK,col);

    int alpha = tqAlpha(col.rgb());
    
    if (alpha < 255)
        SendScintilla(SCI_SETCARETLINEBACKALPHA, alpha);
}


// Set the state of the background colour of the line containing the caret.
void QextScintilla::setCaretLineVisible(bool enable)
{
    SendScintilla(SCI_SETCARETLINEVISIBLE,enable);
}


// Query the read-only state.
bool QextScintilla::isReadOnly()
{
    return SendScintilla(SCI_GETREADONLY);
}


// Set the read-only state.
void QextScintilla::setReadOnly(bool ro)
{
    SendScintilla(SCI_SETREADONLY,ro);
}


// Append the given text.
void QextScintilla::append(const TQString &text)
{
    bool ro = ensureRW();

    if (isUtf8())
    {
        TQCString s = text.utf8();

        SendScintilla(SCI_APPENDTEXT,s.length(),s.data());
    }
    else
    {
        const char *s = text.latin1();

        SendScintilla(SCI_APPENDTEXT,strlen(s),s);
    }

    SendScintilla(SCI_EMPTYUNDOBUFFER);

    setReadOnly(ro);
}


// Insert the given text at the current position.
void QextScintilla::insert(const TQString &text)
{
    bool ro = ensureRW();

    SendScintilla(SCI_BEGINUNDOACTION);

    if (isUtf8())
        SendScintilla(SCI_INSERTTEXT,-1,text.utf8().data());
    else
        SendScintilla(SCI_INSERTTEXT,-1,text.latin1());

    SendScintilla(SCI_ENDUNDOACTION);

    setReadOnly(ro);
}


// Insert the given text at the given position.
void QextScintilla::insertAt(const TQString &text,int line,int index)
{
    bool ro = ensureRW();
    long position = posFromLineIndex(line,index);

    SendScintilla(SCI_BEGINUNDOACTION);

    if (isUtf8())
        SendScintilla(SCI_INSERTTEXT,position,text.utf8().data());
    else
        SendScintilla(SCI_INSERTTEXT,position,text.latin1());

    SendScintilla(SCI_ENDUNDOACTION);

    setReadOnly(ro);
}


// Begin a sequence of undoable actions.
void QextScintilla::beginUndoAction()
{
    SendScintilla(SCI_BEGINUNDOACTION);
}


// End a sequence of undoable actions.
void QextScintilla::endUndoAction()
{
    SendScintilla(SCI_ENDUNDOACTION);
}


// Redo a sequence of actions.
void QextScintilla::redo()
{
    SendScintilla(SCI_REDO);
}


// Undo a sequence of actions.
void QextScintilla::undo()
{
    SendScintilla(SCI_UNDO);
}


// See if there is something to redo.
bool QextScintilla::isRedoAvailable()
{
    return SendScintilla(SCI_CANREDO);
}


// See if there is something to undo.
bool QextScintilla::isUndoAvailable()
{
    return SendScintilla(SCI_CANUNDO);
}


// Return the number of lines.
int QextScintilla::lines()
{
    return SendScintilla(SCI_GETLINECOUNT);
}


// Return the line at a position.
int QextScintilla::lineAt(const TQPoint &pos)
{
    long chpos = SendScintilla(SCI_POSITIONFROMPOINTCLOSE,pos.x(),pos.y());

    if (chpos < 0)
        return -1;

    return SendScintilla(SCI_LINEFROMPOSITION,chpos);
}


// Return the length of a line.
int QextScintilla::lineLength(int line)
{
    if (line < 0 || line >= SendScintilla(SCI_GETLINECOUNT))
        return -1;

    return SendScintilla(SCI_LINELENGTH,line);
}


// Return the length of the current text.
int QextScintilla::length()
{
    return SendScintilla(SCI_GETTEXTLENGTH);
}


// Remove any selected text.
void QextScintilla::removeSelectedText()
{
    SendScintilla(SCI_REPLACESEL,"");
}


// Return the current selected text.
TQString QextScintilla::selectedText()
{
    if (!selText)
        return TQString();

    // Scintilla doesn't tell us the length of the selected text so we use
    // the length of the whole document.
    char *buf = new char[length() + 1];

    SendScintilla(SCI_GETSELTEXT,buf);

    TQString qs = convertText(buf);
    delete[] buf;

    return qs;
}


// Return the current text.
TQString QextScintilla::text()
{
    int buflen = length() + 1;
    char *buf = new char[buflen];

    SendScintilla(SCI_GETTEXT,buflen,buf);

    TQString qs = convertText(buf);
    delete[] buf;

    return qs;
}


// Return the text of a line.
TQString QextScintilla::text(int line)
{
    int line_len = lineLength(line);

    if (line_len < 1)
        return TQString();

    char *buf = new char[line_len + 1];

    SendScintilla(SCI_GETLINE,line,buf);
    buf[line_len] = '\0';

    TQString qs = convertText(buf);
    delete[] buf;

    return qs;
}


// Set the given text.
void QextScintilla::setText(const TQString &text)
{
    bool ro = ensureRW();

    if (isUtf8())
        SendScintilla(SCI_SETTEXT,text.utf8().data());
    else
        SendScintilla(SCI_SETTEXT,text.latin1());

    SendScintilla(SCI_EMPTYUNDOBUFFER);

    setReadOnly(ro);
}


// Get the cursor position
void QextScintilla::getCursorPosition(int *line,int *index)
{
    long pos = SendScintilla(SCI_GETCURRENTPOS);
    long lin = SendScintilla(SCI_LINEFROMPOSITION,pos);
    long linpos = SendScintilla(SCI_POSITIONFROMLINE,lin);

    *line = lin;
    *index = pos - linpos;
}


// Set the cursor position
void QextScintilla::setCursorPosition(int line,int index)
{
    SendScintilla(SCI_GOTOPOS,posFromLineIndex(line,index));
}


// Ensure the cursor is visible.
void QextScintilla::ensureCursorVisible()
{
    SendScintilla(SCI_SCROLLCARET);
}


// Ensure a line is visible.
void QextScintilla::ensureLineVisible(int line)
{
    SendScintilla(SCI_ENSUREVISIBLEENFORCEPOLICY,line);
}


// Copy text to the clipboard.
void QextScintilla::copy()
{
    SendScintilla(SCI_COPY);
}


// Cut text to the clipboard.
void QextScintilla::cut()
{
    SendScintilla(SCI_CUT);
}


// Paste text from the clipboard.
void QextScintilla::paste()
{
    SendScintilla(SCI_PASTE);
}


// Select all text, or deselect any selected text.
void QextScintilla::selectAll(bool select)
{
    if (selText)
        SendScintilla(SCI_SETANCHOR,SendScintilla(SCI_GETCURRENTPOS));
    else
        SendScintilla(SCI_SELECTALL);
}


// Delete all text.
void QextScintilla::clear()
{
    bool ro = ensureRW();

    SendScintilla(SCI_BEGINUNDOACTION);
    SendScintilla(SCI_CLEARALL);
    SendScintilla(SCI_ENDUNDOACTION);

    setReadOnly(ro);
}


// Return the indentation of a line.
int QextScintilla::indentation(int line)
{
    return SendScintilla(SCI_GETLINEINDENTATION,line);
}


// Set the indentation of a line.
void QextScintilla::setIndentation(int line,int indentation)
{
    SendScintilla(SCI_BEGINUNDOACTION);
    SendScintilla(SCI_SETLINEINDENTATION,line,indentation);
    SendScintilla(SCI_ENDUNDOACTION);
}


// Indent a line.
void QextScintilla::indent(int line)
{
    setIndentation(line,indentation(line) + indentWidth());
}


// Unindent a line.
void QextScintilla::unindent(int line)
{
    int newIndent = indentation(line) - indentWidth();

    if (newIndent < 0)
        newIndent = 0;

    setIndentation(line,newIndent);
}


// Return the indentation of the current line.
int QextScintilla::currentIndent()
{
    return indentation(SendScintilla(SCI_LINEFROMPOSITION,SendScintilla(SCI_GETCURRENTPOS)));
}


// Return the current indentation width.
int QextScintilla::indentWidth()
{
    int w = indentationWidth();

    if (w == 0)
        w = tabWidth();

    return w;
}


// Return the state of indentation guides.
bool QextScintilla::indentationGuides()
{
    return SendScintilla(SCI_GETINDENTATIONGUIDES);
}


// Enable and disable indentation guides.
void QextScintilla::setIndentationGuides(bool enable)
{
    SendScintilla(SCI_SETINDENTATIONGUIDES,enable);
}


// Set the background colour of indentation guides.
void QextScintilla::setIndentationGuidesBackgroundColor(const TQColor &col)
{
    SendScintilla(SCI_STYLESETBACK,STYLE_INDENTGUIDE,col);
}


// Set the foreground colour of indentation guides.
void QextScintilla::setIndentationGuidesForegroundColor(const TQColor &col)
{
    SendScintilla(SCI_STYLESETFORE,STYLE_INDENTGUIDE,col);
}


// Return the indentation width.
int QextScintilla::indentationWidth()
{
    return SendScintilla(SCI_GETINDENT);
}


// Set the indentation width.
void QextScintilla::setIndentationWidth(int width)
{
    SendScintilla(SCI_SETINDENT,width);
}


// Return the tab width.
int QextScintilla::tabWidth()
{
    return SendScintilla(SCI_GETTABWIDTH);
}


// Set the tab width.
void QextScintilla::setTabWidth(int width)
{
    SendScintilla(SCI_SETTABWIDTH,width);
}


// Return the effect of the backspace key.
bool QextScintilla::backspaceUnindents()
{
    return SendScintilla(SCI_GETBACKSPACEUNINDENTS);
}


// Set the effect of the backspace key.
void QextScintilla::setBackspaceUnindents(bool unindents)
{
    SendScintilla(SCI_SETBACKSPACEUNINDENTS,unindents);
}


// Return the effect of the tab key.
bool QextScintilla::tabIndents()
{
    return SendScintilla(SCI_GETTABINDENTS);
}


// Set the effect of the tab key.
void QextScintilla::setTabIndents(bool indents)
{
    SendScintilla(SCI_SETTABINDENTS,indents);
}


// Return the indentation use of tabs.
bool QextScintilla::indentationsUseTabs()
{
    return SendScintilla(SCI_GETUSETABS);
}


// Set the indentation use of tabs.
void QextScintilla::setIndentationsUseTabs(bool tabs)
{
    SendScintilla(SCI_SETUSETABS,tabs);
}


// Return the state of line numbers in a margin.
bool QextScintilla::marginLineNumbers(int margin)
{
    return SendScintilla(SCI_GETMARGINTYPEN,margin);
}


// Enable and disable line numbers in a margin.
void QextScintilla::setMarginLineNumbers(int margin,bool lnrs)
{
    SendScintilla(SCI_SETMARGINTYPEN,margin,lnrs ? SC_MARGIN_NUMBER : 0);
}


// Return the marker mask of a margin.
int QextScintilla::marginMarkerMask(int margin)
{
    return SendScintilla(SCI_GETMARGINMASKN,margin);
}


// Set the marker mask of a margin.
void QextScintilla::setMarginMarkerMask(int margin,int mask)
{
    SendScintilla(SCI_SETMARGINMASKN,margin,mask);
}


// Return the state of a margin's sensitivity.
bool QextScintilla::marginSensitivity(int margin)
{
    return SendScintilla(SCI_GETMARGINSENSITIVEN,margin);
}


// Enable and disable a margin's sensitivity.
void QextScintilla::setMarginSensitivity(int margin,bool sens)
{
    SendScintilla(SCI_SETMARGINSENSITIVEN,margin,sens);
}


// Return the width of a margin.
int QextScintilla::marginWidth(int margin)
{
    return SendScintilla(SCI_GETMARGINWIDTHN,margin);
}


// Set the width of a margin.
void QextScintilla::setMarginWidth(int margin,int width)
{
    SendScintilla(SCI_SETMARGINWIDTHN,margin,width);
}


// Set the width of a margin to the width of some text.
void QextScintilla::setMarginWidth(int margin,const TQString &s)
{
    int width;

    if (isUtf8())
        width = SendScintilla(SCI_TEXTWIDTH,STYLE_LINENUMBER,s.utf8().data());
    else
        width = SendScintilla(SCI_TEXTWIDTH,STYLE_LINENUMBER,s.latin1());

    setMarginWidth(margin,width);
}


// Set the background colour of all margins.
void QextScintilla::setMarginsBackgroundColor(const TQColor &col)
{
    handleStylePaperChange(col,STYLE_LINENUMBER);
}


// Set the foreground colour of all margins.
void QextScintilla::setMarginsForegroundColor(const TQColor &col)
{
    handleStyleColorChange(col,STYLE_LINENUMBER);
}


// Set the font of all margins.
void QextScintilla::setMarginsFont(const TQFont &f)
{
    setStylesFont(f,STYLE_LINENUMBER);
}


// Define a marker based on a symbol.
int QextScintilla::markerDefine(MarkerSymbol sym,int mnr)
{
    checkMarker(mnr);

    if (mnr >= 0)
        SendScintilla(SCI_MARKERDEFINE,mnr,static_cast<long>(sym));

    return mnr;
}


// Define a marker based on a character.
int QextScintilla::markerDefine(char ch,int mnr)
{
    checkMarker(mnr);

    if (mnr >= 0)
        SendScintilla(SCI_MARKERDEFINE,mnr,static_cast<long>(SC_MARK_CHARACTER) + ch);

    return mnr;
}


// Define a marker based on a TQPixmap.
int QextScintilla::markerDefine(const TQPixmap *pm,int mnr)
{
    checkMarker(mnr);

    if (mnr >= 0)
        SendScintilla(SCI_MARKERDEFINEPIXMAP,mnr,pm);

    return mnr;
}


// Add a marker to a line.
int QextScintilla::markerAdd(int linenr,int mnr)
{
    if (mnr < 0 || mnr > MARKER_MAX || (allocatedMarkers & (1 << mnr)) == 0)
        return -1;

    return SendScintilla(SCI_MARKERADD,linenr,mnr);
}


// Get the marker mask for a line.
unsigned QextScintilla::markersAtLine(int linenr)
{
    return SendScintilla(SCI_MARKERGET,linenr);
}


// Delete a marker from a line.
void QextScintilla::markerDelete(int linenr,int mnr)
{
    if (mnr <= MARKER_MAX)
    {
        if (mnr < 0)
        {
            unsigned am = allocatedMarkers;

            for (int m = 0; m <= MARKER_MAX; ++m)
            {
                if (am & 1)
                    SendScintilla(SCI_MARKERDELETE,linenr,m);

                am >>= 1;
            }
        }
        else if (allocatedMarkers & (1 << mnr))
            SendScintilla(SCI_MARKERDELETE,linenr,mnr);
    }
}


// Delete a marker from the text.
void QextScintilla::markerDeleteAll(int mnr)
{
    if (mnr <= MARKER_MAX)
    {
        if (mnr < 0)
            SendScintilla(SCI_MARKERDELETEALL,-1);
        else if (allocatedMarkers & (1 << mnr))
            SendScintilla(SCI_MARKERDELETEALL,mnr);
    }
}


// Delete a marker handle from the text.
void QextScintilla::markerDeleteHandle(int mhandle)
{
    SendScintilla(SCI_MARKERDELETEHANDLE,mhandle);
}


// Return the line containing a marker instance.
int QextScintilla::markerLine(int mhandle)
{
    return SendScintilla(SCI_MARKERLINEFROMHANDLE,mhandle);
}


// Search forwards for a marker.
int QextScintilla::markerFindNext(int linenr,unsigned mask)
{
    return SendScintilla(SCI_MARKERNEXT,linenr,mask);
}


// Search backwards for a marker.
int QextScintilla::markerFindPrevious(int linenr,unsigned mask)
{
    return SendScintilla(SCI_MARKERPREVIOUS,linenr,mask);
}


// Set the marker background colour.
void QextScintilla::setMarkerBackgroundColor(const TQColor &col,int mnr)
{
    if (mnr <= MARKER_MAX)
    {
        int alpha = tqAlpha(col.rgb());

        if (mnr < 0)
        {
            unsigned am = allocatedMarkers;

            for (int m = 0; m <= MARKER_MAX; ++m)
            {
                if (am & 1)
                {
                    SendScintilla(SCI_MARKERSETBACK,m,col);

                    if (alpha < 255)
                        SendScintilla(SCI_MARKERSETALPHA, m, alpha);
                }

                am >>= 1;
            }
        }
        else if (allocatedMarkers & (1 << mnr))
        {
            SendScintilla(SCI_MARKERSETBACK,mnr,col);

            if (alpha < 255)
                SendScintilla(SCI_MARKERSETALPHA, mnr, alpha);
        }
    }
}


// Set the marker foreground colour.
void QextScintilla::setMarkerForegroundColor(const TQColor &col,int mnr)
{
    if (mnr <= MARKER_MAX)
    {
        if (mnr < 0)
        {
            unsigned am = allocatedMarkers;

            for (int m = 0; m <= MARKER_MAX; ++m)
            {
                if (am & 1)
                    SendScintilla(SCI_MARKERSETFORE,m,col);

                am >>= 1;
            }
        }
        else if (allocatedMarkers & (1 << mnr))
            SendScintilla(SCI_MARKERSETFORE,mnr,col);
    }
}


// Check a marker, allocating a marker number if necessary.
void QextScintilla::checkMarker(int &mnr)
{
    if (mnr >= 0)
    {
        // Check the explicit marker number isn't already allocated.
        if (mnr > MARKER_MAX || allocatedMarkers & (1 << mnr))
            mnr = -1;
    }
    else
    {
        unsigned am = allocatedMarkers;

        // Find the smallest unallocated marker number.
        for (mnr = 0; mnr <= MARKER_MAX; ++mnr)
        {
            if ((am & 1) == 0)
                break;

            am >>= 1;
        }
    }

    // Define the marker if it is valid.
    if (mnr >= 0)
        allocatedMarkers |= (1 << mnr);
}


// Reset the fold margin colours.
void QextScintilla::resetFoldMarginColors()
{
    SendScintilla(SCI_SETFOLDMARGINHICOLOUR,0,0L);
    SendScintilla(SCI_SETFOLDMARGINCOLOUR,0,0L);
}


// Set the fold margin colours.
void QextScintilla::setFoldMarginColors(const TQColor &fore,const TQColor &back)
{
    SendScintilla(SCI_SETFOLDMARGINHICOLOUR,1,fore);
    SendScintilla(SCI_SETFOLDMARGINCOLOUR,1,back);
}


// Set the call tips background colour.
void QextScintilla::setCallTipsBackgroundColor(const TQColor &col)
{
    SendScintilla(SCI_CALLTIPSETBACK,col);
}


// Set the call tips foreground colour.
void QextScintilla::setCallTipsForegroundColor(const TQColor &col)
{
    SendScintilla(SCI_CALLTIPSETFORE,col);
}


// Set the call tips highlight colour.
void QextScintilla::setCallTipsHighlightColor(const TQColor &col)
{
    SendScintilla(SCI_CALLTIPSETFOREHLT,col);
}


// Set the matched brace background colour.
void QextScintilla::setMatchedBraceBackgroundColor(const TQColor &col)
{
    SendScintilla(SCI_STYLESETBACK,STYLE_BRACELIGHT,col);
}


// Set the matched brace foreground colour.
void QextScintilla::setMatchedBraceForegroundColor(const TQColor &col)
{
    SendScintilla(SCI_STYLESETFORE,STYLE_BRACELIGHT,col);
}


// Set the unmatched brace background colour.
void QextScintilla::setUnmatchedBraceBackgroundColor(const TQColor &col)
{
    SendScintilla(SCI_STYLESETBACK,STYLE_BRACEBAD,col);
}


// Set the unmatched brace foreground colour.
void QextScintilla::setUnmatchedBraceForegroundColor(const TQColor &col)
{
    SendScintilla(SCI_STYLESETFORE,STYLE_BRACEBAD,col);
}


// Set the lexer.
void QextScintilla::setLexer(QextScintillaLexer *lexer)
{
    // Disconnect any previous lexer.
    if (!lex.isNull())
    {
        lex -> disconnect(this);

        SendScintilla(SCI_STYLERESETDEFAULT);
    }

    // Connect up the new lexer.
    lex = lexer;

    if (lex)
    {
        int bits = SendScintilla(SCI_GETSTYLEBITSNEEDED);
        int nrStyles = 1 << bits;

        SendScintilla(SCI_SETSTYLEBITS,bits);

        connect(lex,TQT_SIGNAL(colorChanged(const TQColor &,int)),
            TQT_SLOT(handleStyleColorChange(const TQColor &,int)));
        connect(lex,TQT_SIGNAL(eolFillChanged(bool,int)),
            TQT_SLOT(handleStyleEolFillChange(bool,int)));
        connect(lex,TQT_SIGNAL(fontChanged(const TQFont &,int)),
            TQT_SLOT(handleStyleFontChange(const TQFont &,int)));
        connect(lex,TQT_SIGNAL(paperChanged(const TQColor &,int)),
            TQT_SLOT(handleStylePaperChange(const TQColor &,int)));
        connect(lex,TQT_SIGNAL(propertyChanged(const char *,const char *)),
            TQT_SLOT(handlePropertyChange(const char *,const char *)));

        SendScintilla(SCI_SETLEXERLANGUAGE,lex -> lexer());

        // Set the keywords.  Scintilla allows for sets numbered 0 to
        // KEYWORDSET_MAX (although the lexers only seem to exploit 0
        // to KEYWORDSET_MAX - 1).  We number from 1 in line with
        // SciTE's property files.
        for (int k = 0; k <= KEYWORDSET_MAX; ++k)
        {
            const char *kw = lex -> keywords(k + 1);

            if (kw)
                SendScintilla(SCI_SETKEYWORDS,k,kw);
        }

        // Initialise each style.
        for (int s = 0; s < nrStyles; ++s)
        {
            if (lex -> description(s).isNull())
                continue;

            handleStyleColorChange(lex -> color(s),s);
            handleStyleEolFillChange(lex -> eolFill(s),s);
            handleStyleFontChange(lex -> font(s),s);
            handleStylePaperChange(lex -> paper(s),s);
        }

        // Initialise the properties.
        lex -> refreshProperties();

        // Set the auto-completion fillups if they haven't been
        // explcitly set.
        if (fillups_enabled && !explicit_fillups)
            SendScintilla(SCI_AUTOCSETFILLUPS, lex->autoCompletionFillups());
    }
    else
    {
        SendScintilla(SCI_SETLEXER,SCLEX_NULL);

        setColor(nl_text_colour);
        setPaper(nl_paper_colour);
        setFont(nl_font);
    }
}


// Get the current lexer.
QextScintillaLexer *QextScintilla::lexer() const
{
    return lex;
}


// Handle a change in lexer style foreground colour.
void QextScintilla::handleStyleColorChange(const TQColor &c,int style)
{
    SendScintilla(SCI_STYLESETFORE,style,c);
}


// Handle a change in lexer style end-of-line fill.
void QextScintilla::handleStyleEolFillChange(bool eolfill,int style)
{
    SendScintilla(SCI_STYLESETEOLFILLED,style,eolfill);
}


// Handle a change in lexer style font.
void QextScintilla::handleStyleFontChange(const TQFont &f,int style)
{
    setStylesFont(f,style);

    if (style == lex->defaultStyle())
        setStylesFont(f, STYLE_DEFAULT);

    if (style == lex -> braceStyle())
    {
        setStylesFont(f,STYLE_BRACELIGHT);
        setStylesFont(f,STYLE_BRACEBAD);
    }
}


// Set the font for a style.
void QextScintilla::setStylesFont(const TQFont &f,int style)
{
    SendScintilla(SCI_STYLESETFONT,style,f.family().latin1());
    SendScintilla(SCI_STYLESETSIZE,style,f.pointSize());
    SendScintilla(SCI_STYLESETBOLD,style,f.bold());
    SendScintilla(SCI_STYLESETITALIC,style,f.italic());
    SendScintilla(SCI_STYLESETUNDERLINE,style,f.underline());
}


// Handle a change in lexer style background colour.
void QextScintilla::handleStylePaperChange(const TQColor &c,int style)
{
    SendScintilla(SCI_STYLESETBACK,style,c);
}


// Handle a change in lexer property.
void QextScintilla::handlePropertyChange(const char *prop,const char *val)
{
    SendScintilla(SCI_SETPROPERTY,prop,val);
}


// Handle a change to the user visible user interface.
void QextScintilla::handleUpdateUI()
{
    long newPos = SendScintilla(SCI_GETCURRENTPOS);

    if (newPos != oldPos)
    {
        oldPos = newPos;

        int line = SendScintilla(SCI_LINEFROMPOSITION,newPos);
        int col = SendScintilla(SCI_GETCOLUMN,newPos);

        emit cursorPositionChanged(line,col);
    }

    if (braceMode != NoBraceMatch)
        braceMatch();
}


// Handle brace matching.
void QextScintilla::braceMatch()
{
    long braceAtCaret, braceOpposite;

    findMatchingBrace(braceAtCaret,braceOpposite,braceMode);

    if (braceAtCaret >= 0 && braceOpposite < 0)
    {
        SendScintilla(SCI_BRACEBADLIGHT,braceAtCaret);
                SendScintilla(SCI_SETHIGHLIGHTGUIDE,0UL);
    }
    else
    {
        char chBrace = SendScintilla(SCI_GETCHARAT,braceAtCaret);

        SendScintilla(SCI_BRACEHIGHLIGHT,braceAtCaret,braceOpposite);

        long columnAtCaret = SendScintilla(SCI_GETCOLUMN,braceAtCaret);
                long columnOpposite = SendScintilla(SCI_GETCOLUMN,braceOpposite);

        if (chBrace == ':')
        {
            long lineStart = SendScintilla(SCI_LINEFROMPOSITION,braceAtCaret);
            long indentPos = SendScintilla(SCI_GETLINEINDENTPOSITION,lineStart);
                        long indentPosNext = SendScintilla(SCI_GETLINEINDENTPOSITION,lineStart + 1);

                        columnAtCaret = SendScintilla(SCI_GETCOLUMN,indentPos);

                        long columnAtCaretNext = SendScintilla(SCI_GETCOLUMN,indentPosNext);
                        long indentSize = SendScintilla(SCI_GETINDENT);

            if (columnAtCaretNext - indentSize > 1)
                columnAtCaret = columnAtCaretNext - indentSize;

            if (columnOpposite == 0)
                columnOpposite = columnAtCaret;
        }

        long column = columnAtCaret;

        if (column > columnOpposite)
            column = columnOpposite;

        SendScintilla(SCI_SETHIGHLIGHTGUIDE,column);
    }
}


// Check if the character at a position is a brace.
long QextScintilla::checkBrace(long pos,int brace_style,bool &colonMode)
{
    long brace_pos = -1;
    char ch = SendScintilla(SCI_GETCHARAT,pos);

    if (ch == ':')
    {
        // A bit of a hack.
        if (!lex.isNull() && strcmp(lex -> lexer(),"python") == 0)
        {
            brace_pos = pos;
            colonMode = TRUE;
        }
    }
    else if (ch && strchr("[](){}<>",ch))
    {
        if (brace_style < 0)
            brace_pos = pos;
        else
        {
            int style = SendScintilla(SCI_GETSTYLEAT,pos) & 0x1f;

            if (style == brace_style)
                brace_pos = pos;
        }
    }

    return brace_pos;
}


// Find a brace and it's match.  Return TRUE if the current position is inside
// a pair of braces.
bool QextScintilla::findMatchingBrace(long &brace,long &other,BraceMatch mode)
{
    bool colonMode = FALSE;
    int brace_style = (lex.isNull() ? -1 : lex -> braceStyle());

    brace = -1;
    other = -1;

    long caretPos = SendScintilla(SCI_GETCURRENTPOS);

    if (caretPos > 0)
        brace = checkBrace(caretPos - 1,brace_style,colonMode);

    bool isInside = FALSE;

    if (brace < 0 && mode == SloppyBraceMatch)
    {
        brace = checkBrace(caretPos,brace_style,colonMode);

        if (brace >= 0 && !colonMode)
            isInside = TRUE;
    }

    if (brace >= 0)
    {
        if (colonMode)
        {
            // Find the end of the Python indented block.
            long lineStart = SendScintilla(SCI_LINEFROMPOSITION,brace);
            long lineMaxSubord = SendScintilla(SCI_GETLASTCHILD,lineStart,-1);

            other = SendScintilla(SCI_GETLINEENDPOSITION,lineMaxSubord);
        }
        else
            other = SendScintilla(SCI_BRACEMATCH,brace);

        if (other > brace)
            isInside = !isInside;
    }

    return isInside;
}


// Move to the matching brace.
void QextScintilla::moveToMatchingBrace()
{
    gotoMatchingBrace(FALSE);
}


// Select to the matching brace.
void QextScintilla::selectToMatchingBrace()
{
    gotoMatchingBrace(TRUE);
}


// Move to the matching brace and optionally select the text.
void QextScintilla::gotoMatchingBrace(bool select)
{
    long braceAtCaret;
    long braceOpposite;

    bool isInside = findMatchingBrace(braceAtCaret,braceOpposite,SloppyBraceMatch);

    if (braceOpposite >= 0)
    {
        // Convert the character positions into caret positions based
        // on whether the caret position was inside or outside the
        // braces.
        if (isInside)
        {
            if (braceOpposite > braceAtCaret)
                braceAtCaret++;
            else
                braceOpposite++;
        }
        else
        {
            if (braceOpposite > braceAtCaret)
                braceOpposite++;
            else
                braceAtCaret++;
        }

        ensureLineVisible(SendScintilla(SCI_LINEFROMPOSITION,braceOpposite));

        if (select)
            SendScintilla(SCI_SETSEL,braceAtCaret,braceOpposite);
        else
            SendScintilla(SCI_SETSEL,braceOpposite,braceOpposite);
    }
}


// Return a position from a line number and an index within the line.
long QextScintilla::posFromLineIndex(int line,int index)
{
    long pos = SendScintilla(SCI_POSITIONFROMLINE,line);

    // Allow for multi-byte characters.
    for(int i = 0; i < index; i++)
        pos = SendScintilla(SCI_POSITIONAFTER,pos);

    return pos;
}


// Return a line number and an index within the line from a position.
void QextScintilla::lineIndexFromPos(long pos,int *line,int *index)
{
    long lin = SendScintilla(SCI_LINEFROMPOSITION,pos);
    long linpos = SendScintilla(SCI_POSITIONFROMLINE,lin);

    *line = lin;
    *index = pos - linpos;
}


// Convert a Scintilla string to a TQt Unicode string.
TQString QextScintilla::convertText(const char *s)
{
    if (isUtf8())
        return TQString::fromUtf8(s);

    TQString qs;

    qs.setLatin1(s);

    return qs;
}


// Set the source of the auto-completion list.
void QextScintilla::setAutoCompletionSource(AutoCompletionSource source)
{
    acSource = source;
}


// Set the threshold for automatic auto-completion.
void QextScintilla::setAutoCompletionThreshold(int thresh)
{
    acThresh = thresh;
}


// Set the auto-completion start characters.
void QextScintilla::setAutoCompletionStartCharacters(const char *start)
{
    acStart = start;
}


// Set the APIs for auto-completion.
void QextScintilla::setAutoCompletionAPIs(QextScintillaAPIs *apis)
{
    acAPIs = apis;
}


// Explicitly auto-complete from all sources.
void QextScintilla::autoCompleteFromAll()
{
    startAutoCompletion(AcsAll, FALSE, showSingle);
}


// Explicitly auto-complete from the APIs.
void QextScintilla::autoCompleteFromAPIs()
{
    startAutoCompletion(AcsAPIs, FALSE, showSingle);
}


// Explicitly auto-complete from the document.
void QextScintilla::autoCompleteFromDocument()
{
    // If we are not in a word then ignore.
    if (currentCharInWord())
        startAutoCompletion(AcsDocument, FALSE, showSingle);
}


// Return TRUE if the current character (ie. the one before the carat) is part
// of a word.
bool QextScintilla::currentCharInWord()
{
    long pos = SendScintilla(SCI_GETCURRENTPOS);

    if (pos <= 0)
        return FALSE;

    return isWordChar(SendScintilla(SCI_GETCHARAT,pos - 1));
}


// Check if a character can be in a word.
bool QextScintilla::isWordChar(char ch) const
{
    const char *word_chars = 0;

    if (!lex.isNull())
        word_chars = lex->wordCharacters();

    if (!word_chars)
        word_chars = defaultWordChars;

    return (strchr(word_chars, ch) != NULL);
}


// Recolour the document.
void QextScintilla::recolor(int start,int end)
{
    SendScintilla(SCI_COLOURISE,start,end);
}


// Registered an image.
void QextScintilla::registerImage(int id,const TQPixmap *pm)
{
    SendScintilla(SCI_REGISTERIMAGE,id,pm);
}


// Clear all registered images.
void QextScintilla::clearRegisteredImages()
{
    SendScintilla(SCI_CLEARREGISTEREDIMAGES);
}


// Set the fill-up characters for auto-completion.
void QextScintilla::setAutoCompletionFillups(const char *fillups)
{
    if (!fillups)
        fillups = "";

    SendScintilla(SCI_AUTOCSETFILLUPS, fillups);
    fillups_enabled = explicit_fillups = TRUE;

    // Save them in case we need to reenable them at some point.
    saved_fillups = fillups;
}


// Enable/disable fill-ups for auto-completion.
void QextScintilla::setAutoCompletionFillupsEnabled(bool enabled)
{
    const char *fillups;

    if (!enabled)
        fillups = "";
    else if (!explicit_fillups && !lex.isNull())
        fillups = lex->autoCompletionFillups();
    else
        fillups = saved_fillups.data();

    SendScintilla(SCI_AUTOCSETFILLUPS, fillups);
    fillups_enabled = enabled;
}


// Return the state of fill-ups for auto-completion.
bool QextScintilla::autoCompletionFillupsEnabled()
{
    return fillups_enabled;
}


// Set the case sensitivity for auto-completion.
void QextScintilla::setAutoCompletionCaseSensitivity(bool cs)
{
    SendScintilla(SCI_AUTOCSETIGNORECASE,!cs);
}


// Return the case sensitivity for auto-completion.
bool QextScintilla::autoCompletionCaseSensitivity()
{
    return !SendScintilla(SCI_AUTOCGETIGNORECASE);
}


// Set the replace word mode for auto-completion.
void QextScintilla::setAutoCompletionReplaceWord(bool replace)
{
    SendScintilla(SCI_AUTOCSETDROPRESTOFWORD,replace);
}


// Return the replace word mode for auto-completion.
bool QextScintilla::autoCompletionReplaceWord()
{
    return SendScintilla(SCI_AUTOCGETDROPRESTOFWORD);
}


// Set the single item mode for auto-completion.
void QextScintilla::setAutoCompletionShowSingle(bool single)
{
    showSingle = single;
}


// Return the single item mode for auto-completion.
bool QextScintilla::autoCompletionShowSingle()
{
    return showSingle;
}


// Set the APIs for call tips.
void QextScintilla::setCallTipsAPIs(QextScintillaAPIs *apis)
{
    ctAPIs = apis;
}


// Set maximum number of call tips displayed.
void QextScintilla::setCallTipsVisible(int nr)
{
    maxCallTips = nr;
}


// Set the document to display.
void QextScintilla::setDocument(const QextScintillaDocument &document)
{
    if (doc.pdoc != document.pdoc)
    {
        doc.undisplay(this);
        doc.attach(document);
        doc.display(this,&document);
    }
}


// Ensure the document is read-write and return True if was was read-only.
bool QextScintilla::ensureRW()
{
    bool ro = isReadOnly();

    if (ro)
        setReadOnly(FALSE);

    return ro;
}


// Return the number of the first visible line.
int QextScintilla::firstVisibleLine()
{
    return SendScintilla(SCI_GETFIRSTVISIBLELINE);
}


// Return the height in pixels of the text in a particular line.
int QextScintilla::textHeight(int linenr)
{
    return SendScintilla(SCI_TEXTHEIGHT, linenr);
}


// See if auto-completion or user list is active.
bool QextScintilla::isListActive()
{
    return SendScintilla(SCI_AUTOCACTIVE);
}


// Cancel any current auto-completion or user list.
void QextScintilla::cancelList()
{
    SendScintilla(SCI_AUTOCCANCEL);
}


// Display a user list.
void QextScintilla::showUserList(int id, const TQStringList &list)
{
    // Sanity check to make sure auto-completion doesn't get confused.
    if (id <= 0)
        return;

    const char sep = '\x03';

    SendScintilla(SCI_AUTOCSETSEPARATOR, sep);
    SendScintilla(SCI_USERLISTSHOW, id, list.join(TQChar(sep)).latin1());
}


// Translate the SCN_USERLISTSELECTION notification into something more useful.
void QextScintilla::handleUserListSelection(const char *text, int id)
{
    emit userListActivated(id, TQString(text));
}