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
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; coding: utf-8 -*- */
/* gtksourcebuffer.c
* This file is part of GtkSourceView
*
* Copyright (C) 1999-2002 - Mikael Hermansson <tyan@linux.se>,
* Chris Phelps <chicane@reninet.com> and
* Jeroen Zwartepoorte <jeroen@xs4all.nl>
* Copyright (C) 2003 - Paolo Maggi <paolo.maggi@polito.it> and
* Gustavo Giráldez <gustavo.giraldez@gmx.net>
*
* GtkSourceView is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* GtkSourceView 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <string.h>
#include <gtk/gtk.h>
#include "gtksourcebuffer.h"
#include "gtksourcebuffer-private.h"
#include "gtksourcelanguage.h"
#include "gtksourcelanguage-private.h"
#include "gtksourceundomanager.h"
#include "gtksourceundomanagerdefault.h"
#include "gtksourcestylescheme.h"
#include "gtksourcestyleschememanager.h"
#include "gtksourcestyle-private.h"
#include "gtksourcemark.h"
#include "gtksourcemarkssequence.h"
#include "gtksourcesearchcontext.h"
#include "gtksourceview-i18n.h"
#include "gtksourceview-typebuiltins.h"
/**
* SECTION:buffer
* @Short_description: Buffer object for GtkSourceView
* @Title: GtkSourceBuffer
* @See_also: #GtkTextBuffer, #GtkSourceView
*
* The #GtkSourceBuffer object is the model for #GtkSourceView widgets.
* It extends the #GtkTextBuffer object by adding features useful to display
* and edit source code such as syntax highlighting and bracket matching. It
* also implements support for undo/redo operations, and for the search and
* replace.
*
* To create a #GtkSourceBuffer use gtk_source_buffer_new() or
* gtk_source_buffer_new_with_language(). The second form is just a convenience
* function which allows you to initially set a #GtkSourceLanguage.
*
* By default highlighting is enabled, but you can disable it with
* gtk_source_buffer_set_highlight_syntax().
*
* # Undo and Redo
*
* A custom #GtkSourceUndoManager can be implemented and set with
* gtk_source_buffer_set_undo_manager(). However the default implementation
* should be suitable for most uses. By default, actions that can be undone or
* redone are defined as groups of operations between a call to
* gtk_text_buffer_begin_user_action() and gtk_text_buffer_end_user_action(). In
* general, this happens whenever the user presses any key which modifies the
* buffer. But the default undo manager will try to merge similar consecutive
* actions, such as multiple character insertions on the same line, into one
* action. But, inserting a newline starts a new action.
*
* The default undo manager remembers the "modified" state of the buffer, and
* restore it when an action is undone or redone. It can be useful in a text
* editor to know whether the file is saved. See gtk_text_buffer_get_modified()
* and gtk_text_buffer_set_modified().
*
* # Context Classes
*
* It is possible to retrieve some information from the syntax highlighting
* engine. There are currently three default context classes that are
* applied to regions of a #GtkSourceBuffer:
* - <emphasis>comment</emphasis>: the region delimits a comment;
* - <emphasis>string</emphasis>: the region delimits a string;
* - <emphasis>no-spell-check</emphasis>: the region should not be spell checked.
*
* Custom language definition files can create their own context classes,
* since the functions like gtk_source_buffer_iter_has_context_class() take
* a string parameter as the context class.
*/
/*
#define ENABLE_DEBUG
#define ENABLE_PROFILE
*/
#undef ENABLE_DEBUG
#undef ENABLE_PROFILE
#ifdef ENABLE_DEBUG
#define DEBUG(x) (x)
#else
#define DEBUG(x)
#endif
#ifdef ENABLE_PROFILE
#define PROFILE(x) (x)
#else
#define PROFILE(x)
#endif
/* For bracket matching */
#define MAX_CHARS_BEFORE_FINDING_A_MATCH 10000
#define TAG_CONTEXT_CLASS_NAME "GtkSourceViewTagContextClassName"
/* Signals */
enum {
HIGHLIGHT_UPDATED,
SOURCE_MARK_UPDATED,
UNDO,
REDO,
BRACKET_MATCHED,
LAST_SIGNAL
};
/* Properties */
enum {
PROP_0,
PROP_CAN_UNDO,
PROP_CAN_REDO,
PROP_HIGHLIGHT_SYNTAX,
PROP_HIGHLIGHT_MATCHING_BRACKETS,
PROP_MAX_UNDO_LEVELS,
PROP_LANGUAGE,
PROP_STYLE_SCHEME,
PROP_UNDO_MANAGER,
PROP_IMPLICIT_TRAILING_NEWLINE
};
struct _GtkSourceBufferPrivate
{
GtkTextTag *bracket_match_tag;
GtkTextMark *bracket_mark_cursor;
GtkTextMark *bracket_mark_match;
GtkSourceBracketMatchType bracket_match;
/* Hash table: category -> MarksSequence */
GHashTable *source_marks;
GtkSourceMarksSequence *all_source_marks;
GtkSourceLanguage *language;
GtkSourceEngine *highlight_engine;
GtkSourceStyleScheme *style_scheme;
GtkSourceUndoManager *undo_manager;
gint max_undo_levels;
GList *search_contexts;
GtkTextTag *invalid_char_tag;
guint highlight_syntax : 1;
guint highlight_brackets : 1;
guint constructed : 1;
guint allow_bracket_match : 1;
guint implicit_trailing_newline : 1;
};
G_DEFINE_TYPE_WITH_PRIVATE (GtkSourceBuffer, gtk_source_buffer, GTK_TYPE_TEXT_BUFFER)
static guint buffer_signals[LAST_SIGNAL];
static void gtk_source_buffer_dispose (GObject *object);
static void gtk_source_buffer_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec);
static void gtk_source_buffer_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec);
static void gtk_source_buffer_can_undo_handler (GtkSourceUndoManager *manager,
GtkSourceBuffer *buffer);
static void gtk_source_buffer_can_redo_handler (GtkSourceUndoManager *manager,
GtkSourceBuffer *buffer);
static void gtk_source_buffer_real_insert_text (GtkTextBuffer *buffer,
GtkTextIter *iter,
const gchar *text,
gint len);
static void gtk_source_buffer_real_insert_pixbuf (GtkTextBuffer *buffer,
GtkTextIter *pos,
GdkPixbuf *pixbuf);
static void gtk_source_buffer_real_insert_anchor (GtkTextBuffer *buffer,
GtkTextIter *pos,
GtkTextChildAnchor *anchor);
static void gtk_source_buffer_real_delete_range (GtkTextBuffer *buffer,
GtkTextIter *iter,
GtkTextIter *end);
static void gtk_source_buffer_real_mark_set (GtkTextBuffer *buffer,
const GtkTextIter *location,
GtkTextMark *mark);
static void gtk_source_buffer_real_apply_tag (GtkTextBuffer *buffer,
GtkTextTag *tag,
const GtkTextIter *start,
const GtkTextIter *end);
static void gtk_source_buffer_real_mark_deleted (GtkTextBuffer *buffer,
GtkTextMark *mark);
static gboolean gtk_source_buffer_find_bracket_match_with_limit (GtkSourceBuffer *buffer,
GtkTextIter *orig,
GtkSourceBracketMatchType *result,
gint max_chars);
static void gtk_source_buffer_real_undo (GtkSourceBuffer *buffer);
static void gtk_source_buffer_real_redo (GtkSourceBuffer *buffer);
static void
gtk_source_buffer_constructed (GObject *object)
{
GtkSourceBuffer *buffer = GTK_SOURCE_BUFFER (object);
/* we need to know that the tag-table was set */
buffer->priv->constructed = TRUE;
if (buffer->priv->undo_manager == NULL)
{
/* This will install the default undo manager */
gtk_source_buffer_set_undo_manager (buffer, NULL);
}
G_OBJECT_CLASS (gtk_source_buffer_parent_class)->constructed (object);
}
static void
gtk_source_buffer_class_init (GtkSourceBufferClass *klass)
{
GObjectClass *object_class;
GtkTextBufferClass *tb_class;
GType param_types[2];
object_class = G_OBJECT_CLASS (klass);
tb_class = GTK_TEXT_BUFFER_CLASS (klass);
object_class->constructed = gtk_source_buffer_constructed;
object_class->dispose = gtk_source_buffer_dispose;
object_class->get_property = gtk_source_buffer_get_property;
object_class->set_property = gtk_source_buffer_set_property;
tb_class->delete_range = gtk_source_buffer_real_delete_range;
tb_class->insert_text = gtk_source_buffer_real_insert_text;
tb_class->insert_pixbuf = gtk_source_buffer_real_insert_pixbuf;
tb_class->insert_child_anchor = gtk_source_buffer_real_insert_anchor;
tb_class->apply_tag = gtk_source_buffer_real_apply_tag;
tb_class->mark_set = gtk_source_buffer_real_mark_set;
tb_class->mark_deleted = gtk_source_buffer_real_mark_deleted;
klass->undo = gtk_source_buffer_real_undo;
klass->redo = gtk_source_buffer_real_redo;
/**
* GtkSourceBuffer:highlight-syntax:
*
* Whether to highlight syntax in the buffer.
*/
g_object_class_install_property (object_class,
PROP_HIGHLIGHT_SYNTAX,
g_param_spec_boolean ("highlight-syntax",
_("Highlight Syntax"),
_("Whether to highlight syntax "
"in the buffer"),
TRUE,
G_PARAM_READWRITE));
/**
* GtkSourceBuffer:highlight-matching-brackets:
*
* Whether to highlight matching brackets in the buffer.
*/
g_object_class_install_property (object_class,
PROP_HIGHLIGHT_MATCHING_BRACKETS,
g_param_spec_boolean ("highlight-matching-brackets",
_("Highlight Matching Brackets"),
_("Whether to highlight matching brackets"),
TRUE,
G_PARAM_READWRITE));
/**
* GtkSourceBuffer:max-undo-levels:
*
* Number of undo levels for the buffer. -1 means no limit. This property
* will only affect the default undo manager.
*/
g_object_class_install_property (object_class,
PROP_MAX_UNDO_LEVELS,
g_param_spec_int ("max-undo-levels",
_("Maximum Undo Levels"),
_("Number of undo levels for "
"the buffer"),
-1,
G_MAXINT,
-1,
G_PARAM_READWRITE));
g_object_class_install_property (object_class,
PROP_LANGUAGE,
g_param_spec_object ("language",
/* Translators: throughout GtkSourceView "language" stands
* for "programming language", not "spoken language" */
_("Language"),
_("Language object to get "
"highlighting patterns from"),
GTK_SOURCE_TYPE_LANGUAGE,
G_PARAM_READWRITE));
g_object_class_install_property (object_class,
PROP_CAN_UNDO,
g_param_spec_boolean ("can-undo",
_("Can undo"),
_("Whether Undo operation is possible"),
FALSE,
G_PARAM_READABLE));
g_object_class_install_property (object_class,
PROP_CAN_REDO,
g_param_spec_boolean ("can-redo",
_("Can redo"),
_("Whether Redo operation is possible"),
FALSE,
G_PARAM_READABLE));
/**
* GtkSourceBuffer:style-scheme:
*
* Style scheme. It contains styles for syntax highlighting, optionally
* foreground, background, cursor color, current line color, and matching
* brackets style.
*/
g_object_class_install_property (object_class,
PROP_STYLE_SCHEME,
g_param_spec_object ("style_scheme",
_("Style scheme"),
_("Style scheme"),
GTK_SOURCE_TYPE_STYLE_SCHEME,
G_PARAM_READWRITE));
g_object_class_install_property (object_class,
PROP_UNDO_MANAGER,
g_param_spec_object ("undo-manager",
_("Undo manager"),
_("The buffer undo manager"),
GTK_SOURCE_TYPE_UNDO_MANAGER,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
/**
* GtkSourceBuffer:implicit-trailing-newline:
*
* Whether the buffer has an implicit trailing newline. See
* gtk_source_buffer_set_implicit_trailing_newline().
*
* Since: 3.14
*/
g_object_class_install_property (object_class,
PROP_IMPLICIT_TRAILING_NEWLINE,
g_param_spec_boolean ("implicit-trailing-newline",
_("Implicit trailing newline"),
"",
TRUE,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT |
G_PARAM_STATIC_STRINGS));
param_types[0] = GTK_TYPE_TEXT_ITER | G_SIGNAL_TYPE_STATIC_SCOPE;
param_types[1] = GTK_TYPE_TEXT_ITER | G_SIGNAL_TYPE_STATIC_SCOPE;
/**
* GtkSourceBuffer::highlight-updated:
* @buffer: the buffer that received the signal
* @start: the start of the updated region
* @end: the end of the updated region
*
* The ::highlight-updated signal is emitted when the syntax
* highlighting is updated in a certain region of the @buffer. This
* signal is useful to be notified when a context class region is
* updated (e.g. the no-spell-check context class).
*/
buffer_signals[HIGHLIGHT_UPDATED] =
g_signal_newv ("highlight_updated",
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_LAST,
NULL, NULL, NULL, NULL,
G_TYPE_NONE,
2, param_types);
/**
* GtkSourceBuffer::source-mark-updated:
* @buffer: the buffer that received the signal
* @mark: the #GtkSourceMark
*
* The ::source_mark_updated signal is emitted each time
* a mark is added to, moved or removed from the @buffer.
**/
buffer_signals[SOURCE_MARK_UPDATED] =
g_signal_new ("source_mark_updated",
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_LAST,
0,
NULL, NULL, NULL,
G_TYPE_NONE,
1, GTK_TYPE_TEXT_MARK);
/**
* GtkSourceBuffer::undo:
* @buffer: the buffer that received the signal
*
* The ::undo signal is emitted to undo the last user action which
* modified the buffer.
*/
buffer_signals[UNDO] =
g_signal_new ("undo",
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (GtkSourceBufferClass, undo),
NULL, NULL, NULL,
G_TYPE_NONE, 0);
/**
* GtkSourceBuffer::redo:
* @buffer: the buffer that received the signal
*
* The ::redo signal is emitted to redo the last undo operation.
*/
buffer_signals[REDO] =
g_signal_new ("redo",
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (GtkSourceBufferClass, redo),
NULL, NULL, NULL,
G_TYPE_NONE, 0);
/**
* GtkSourceBuffer::bracket-matched:
* @buffer: a #GtkSourceBuffer.
* @iter: iterator to initialize.
* @state: state of bracket matching
*
* Sets @iter to a valid iterator pointing to the matching bracket
* if @state is #GTK_SOURCE_BRACKET_MATCH_FOUND. Otherwise @iter is
* meaningless.
*
* Since: 2.12
*/
buffer_signals[BRACKET_MATCHED] =
g_signal_new ("bracket-matched",
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (GtkSourceBufferClass, bracket_matched),
NULL, NULL, NULL,
G_TYPE_NONE, 2,
GTK_TYPE_TEXT_ITER,
GTK_SOURCE_TYPE_BRACKET_MATCH_TYPE);
}
static void
set_undo_manager (GtkSourceBuffer *buffer,
GtkSourceUndoManager *manager)
{
if (manager == buffer->priv->undo_manager)
{
return;
}
if (buffer->priv->undo_manager != NULL)
{
g_signal_handlers_disconnect_by_func (buffer->priv->undo_manager,
G_CALLBACK (gtk_source_buffer_can_undo_handler),
buffer);
g_signal_handlers_disconnect_by_func (buffer->priv->undo_manager,
G_CALLBACK (gtk_source_buffer_can_redo_handler),
buffer);
g_object_unref (buffer->priv->undo_manager);
buffer->priv->undo_manager = NULL;
}
if (manager != NULL)
{
buffer->priv->undo_manager = g_object_ref (manager);
g_signal_connect (buffer->priv->undo_manager,
"can-undo-changed",
G_CALLBACK (gtk_source_buffer_can_undo_handler),
buffer);
g_signal_connect (buffer->priv->undo_manager,
"can-redo-changed",
G_CALLBACK (gtk_source_buffer_can_redo_handler),
buffer);
/* Notify possible changes in the can-undo/redo state */
g_object_notify (G_OBJECT (buffer), "can-undo");
g_object_notify (G_OBJECT (buffer), "can-redo");
}
}
static void
search_context_weak_notify_cb (GtkSourceBuffer *buffer,
GObject *search_context)
{
buffer->priv->search_contexts = g_list_remove (buffer->priv->search_contexts,
search_context);
}
static void
gtk_source_buffer_init (GtkSourceBuffer *buffer)
{
GtkSourceBufferPrivate *priv = gtk_source_buffer_get_instance_private (buffer);
buffer->priv = priv;
priv->highlight_syntax = TRUE;
priv->highlight_brackets = TRUE;
priv->bracket_mark_cursor = NULL;
priv->bracket_mark_match = NULL;
priv->bracket_match = GTK_SOURCE_BRACKET_MATCH_NONE;
priv->max_undo_levels = -1;
priv->source_marks = g_hash_table_new_full (g_str_hash,
g_str_equal,
(GDestroyNotify)g_free,
(GDestroyNotify)g_object_unref);
priv->all_source_marks = _gtk_source_marks_sequence_new (GTK_TEXT_BUFFER (buffer));
priv->style_scheme = _gtk_source_style_scheme_get_default ();
if (priv->style_scheme != NULL)
{
g_object_ref (priv->style_scheme);
}
}
static void
gtk_source_buffer_dispose (GObject *object)
{
GtkSourceBuffer *buffer;
GList *l;
g_return_if_fail (object != NULL);
g_return_if_fail (GTK_SOURCE_IS_BUFFER (object));
buffer = GTK_SOURCE_BUFFER (object);
if (buffer->priv->undo_manager != NULL)
{
set_undo_manager (buffer, NULL);
}
if (buffer->priv->highlight_engine != NULL)
{
_gtk_source_engine_attach_buffer (buffer->priv->highlight_engine, NULL);
}
g_clear_object (&buffer->priv->highlight_engine);
g_clear_object (&buffer->priv->language);
g_clear_object (&buffer->priv->style_scheme);
for (l = buffer->priv->search_contexts; l != NULL; l = l->next)
{
GtkSourceSearchContext *search_context = l->data;
g_object_weak_unref (G_OBJECT (search_context),
(GWeakNotify)search_context_weak_notify_cb,
buffer);
}
g_list_free (buffer->priv->search_contexts);
buffer->priv->search_contexts = NULL;
g_clear_object (&buffer->priv->all_source_marks);
if (buffer->priv->source_marks != NULL)
{
g_hash_table_unref (buffer->priv->source_marks);
buffer->priv->source_marks = NULL;
}
G_OBJECT_CLASS (gtk_source_buffer_parent_class)->dispose (object);
}
static void
gtk_source_buffer_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
GtkSourceBuffer *source_buffer;
g_return_if_fail (GTK_SOURCE_IS_BUFFER (object));
source_buffer = GTK_SOURCE_BUFFER (object);
switch (prop_id)
{
case PROP_HIGHLIGHT_SYNTAX:
gtk_source_buffer_set_highlight_syntax (source_buffer,
g_value_get_boolean (value));
break;
case PROP_HIGHLIGHT_MATCHING_BRACKETS:
gtk_source_buffer_set_highlight_matching_brackets (source_buffer,
g_value_get_boolean (value));
break;
case PROP_MAX_UNDO_LEVELS:
gtk_source_buffer_set_max_undo_levels (source_buffer,
g_value_get_int (value));
break;
case PROP_LANGUAGE:
gtk_source_buffer_set_language (source_buffer,
g_value_get_object (value));
break;
case PROP_STYLE_SCHEME:
gtk_source_buffer_set_style_scheme (source_buffer,
g_value_get_object (value));
break;
case PROP_UNDO_MANAGER:
gtk_source_buffer_set_undo_manager (source_buffer,
g_value_get_object (value));
break;
case PROP_IMPLICIT_TRAILING_NEWLINE:
gtk_source_buffer_set_implicit_trailing_newline (source_buffer,
g_value_get_boolean (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gtk_source_buffer_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
GtkSourceBuffer *source_buffer;
g_return_if_fail (GTK_SOURCE_IS_BUFFER (object));
source_buffer = GTK_SOURCE_BUFFER (object);
switch (prop_id)
{
case PROP_HIGHLIGHT_SYNTAX:
g_value_set_boolean (value,
source_buffer->priv->highlight_syntax);
break;
case PROP_HIGHLIGHT_MATCHING_BRACKETS:
g_value_set_boolean (value,
source_buffer->priv->highlight_brackets);
break;
case PROP_MAX_UNDO_LEVELS:
g_value_set_int (value,
source_buffer->priv->max_undo_levels);
break;
case PROP_LANGUAGE:
g_value_set_object (value, source_buffer->priv->language);
break;
case PROP_STYLE_SCHEME:
g_value_set_object (value, source_buffer->priv->style_scheme);
break;
case PROP_CAN_UNDO:
g_value_set_boolean (value, gtk_source_buffer_can_undo (source_buffer));
break;
case PROP_CAN_REDO:
g_value_set_boolean (value, gtk_source_buffer_can_redo (source_buffer));
break;
case PROP_UNDO_MANAGER:
g_value_set_object (value, source_buffer->priv->undo_manager);
break;
case PROP_IMPLICIT_TRAILING_NEWLINE:
g_value_set_boolean (value, source_buffer->priv->implicit_trailing_newline);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
/**
* gtk_source_buffer_new:
* @table: (allow-none): a #GtkTextTagTable, or %NULL to create a new one.
*
* Creates a new source buffer.
*
* Return value: a new source buffer.
**/
GtkSourceBuffer *
gtk_source_buffer_new (GtkTextTagTable *table)
{
return g_object_new (GTK_SOURCE_TYPE_BUFFER,
"tag-table", table,
NULL);
}
/**
* gtk_source_buffer_new_with_language:
* @language: a #GtkSourceLanguage.
*
* Creates a new source buffer using the highlighting patterns in
* @language. This is equivalent to creating a new source buffer with
* a new tag table and then calling gtk_source_buffer_set_language().
*
* Return value: a new source buffer which will highlight text
* according to the highlighting patterns in @language.
**/
GtkSourceBuffer *
gtk_source_buffer_new_with_language (GtkSourceLanguage *language)
{
g_return_val_if_fail (GTK_SOURCE_IS_LANGUAGE (language), NULL);
return g_object_new (GTK_SOURCE_TYPE_BUFFER,
"tag-table", NULL,
"language", language,
NULL);
}
static void
gtk_source_buffer_can_undo_handler (GtkSourceUndoManager *manager,
GtkSourceBuffer *buffer)
{
g_return_if_fail (GTK_SOURCE_IS_BUFFER (buffer));
g_object_notify (G_OBJECT (buffer), "can-undo");
}
static void
gtk_source_buffer_can_redo_handler (GtkSourceUndoManager *manager,
GtkSourceBuffer *buffer)
{
g_return_if_fail (GTK_SOURCE_IS_BUFFER (buffer));
g_object_notify (G_OBJECT (buffer), "can-redo");
}
static void
update_bracket_match_style (GtkSourceBuffer *buffer)
{
if (buffer->priv->bracket_match_tag != NULL)
{
GtkSourceStyle *style = NULL;
if (buffer->priv->style_scheme)
style = _gtk_source_style_scheme_get_matching_brackets_style (buffer->priv->style_scheme);
_gtk_source_style_apply (style, buffer->priv->bracket_match_tag);
}
}
static GtkTextTag *
get_bracket_match_tag (GtkSourceBuffer *buffer)
{
if (buffer->priv->bracket_match_tag == NULL)
{
buffer->priv->bracket_match_tag =
gtk_text_buffer_create_tag (GTK_TEXT_BUFFER (buffer),
NULL,
NULL);
update_bracket_match_style (buffer);
}
return buffer->priv->bracket_match_tag;
}
/*
* This is private, just used by the compositor to not print bracket
* matches. Note that unlike get_bracket_match_tag() it returns NULL
* if the tag is not set.
*/
GtkTextTag *
_gtk_source_buffer_get_bracket_match_tag (GtkSourceBuffer *buffer)
{
return buffer->priv->bracket_match_tag;
}
static gunichar
bracket_pair (gunichar base_char, gint *direction)
{
gint dir;
gunichar pair;
switch ((int)base_char)
{
case '{':
dir = 1;
pair = '}';
break;
case '(':
dir = 1;
pair = ')';
break;
case '[':
dir = 1;
pair = ']';
break;
case '<':
dir = 1;
pair = '>';
break;
case '}':
dir = -1;
pair = '{';
break;
case ')':
dir = -1;
pair = '(';
break;
case ']':
dir = -1;
pair = '[';
break;
case '>':
dir = -1;
pair = '<';
break;
default:
dir = 0;
pair = 0;
break;
}
/* Let direction be NULL if we don't care */
if (direction != NULL)
*direction = dir;
return pair;
}
static void
gtk_source_buffer_move_cursor (GtkTextBuffer *buffer,
const GtkTextIter *iter,
GtkTextMark *mark)
{
GtkSourceBuffer *source_buffer;
GtkTextIter start, end;
gunichar cursor_char;
GtkSourceBracketMatchType previous_state;
g_return_if_fail (GTK_SOURCE_IS_BUFFER (buffer));
g_return_if_fail (iter != NULL);
g_return_if_fail (mark != NULL);
g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
if (mark != gtk_text_buffer_get_insert (buffer))
return;
source_buffer = GTK_SOURCE_BUFFER (buffer);
if (source_buffer->priv->bracket_match == GTK_SOURCE_BRACKET_MATCH_FOUND)
{
gtk_text_buffer_get_iter_at_mark (buffer,
&start,
source_buffer->priv->bracket_mark_match);
gtk_text_buffer_get_iter_at_mark (buffer,
&end,
source_buffer->priv->bracket_mark_cursor);
gtk_text_iter_order (&start, &end);
gtk_text_iter_forward_char (&end);
gtk_text_buffer_remove_tag (buffer,
source_buffer->priv->bracket_match_tag,
&start,
&end);
}
if (!source_buffer->priv->highlight_brackets)
return;
start = *iter;
previous_state = source_buffer->priv->bracket_match;
if (!gtk_source_buffer_find_bracket_match_with_limit (source_buffer,
&start,
&source_buffer->priv->bracket_match,
MAX_CHARS_BEFORE_FINDING_A_MATCH))
{
/* don't emit the signal at all if chars at previous and current
positions are nonbrackets. */
if (previous_state != GTK_SOURCE_BRACKET_MATCH_NONE ||
source_buffer->priv->bracket_match != GTK_SOURCE_BRACKET_MATCH_NONE)
{
g_signal_emit (source_buffer,
buffer_signals[BRACKET_MATCHED],
0,
&end,
source_buffer->priv->bracket_match);
}
}
else
{
g_signal_emit (source_buffer,
buffer_signals[BRACKET_MATCHED],
0,
&start,
source_buffer->priv->bracket_match);
/* allow_bracket_match will allow the bracket match tag to be
applied to the buffer. See apply_tag_real for more
information */
source_buffer->priv->allow_bracket_match = TRUE;
/* Mark matching bracket */
if (!source_buffer->priv->bracket_mark_match)
{
source_buffer->priv->bracket_mark_match =
gtk_text_buffer_create_mark (buffer,
NULL,
&start,
TRUE);
}
else
{
gtk_text_buffer_move_mark (buffer,
source_buffer->priv->bracket_mark_match,
&start);
}
end = start;
gtk_text_iter_forward_char (&end);
gtk_text_buffer_apply_tag (buffer,
get_bracket_match_tag (source_buffer),
&start,
&end);
/* Mark the bracket near the cursor */
start = *iter;
cursor_char = gtk_text_iter_get_char (&start);
if (bracket_pair (cursor_char, NULL) == 0)
gtk_text_iter_backward_char (&start);
if (!source_buffer->priv->bracket_mark_cursor)
{
source_buffer->priv->bracket_mark_cursor =
gtk_text_buffer_create_mark (buffer,
NULL,
&start,
FALSE);
}
else
{
gtk_text_buffer_move_mark (buffer,
source_buffer->priv->bracket_mark_cursor,
&start);
}
end = start;
gtk_text_iter_forward_char (&end);
gtk_text_buffer_apply_tag (buffer,
get_bracket_match_tag (source_buffer),
&start,
&end);
source_buffer->priv->allow_bracket_match = FALSE;
}
}
static void
gtk_source_buffer_content_inserted (GtkTextBuffer *buffer,
gint start_offset,
gint end_offset)
{
GtkTextMark *mark;
GtkTextIter insert_iter;
GtkSourceBuffer *source_buffer = GTK_SOURCE_BUFFER (buffer);
mark = gtk_text_buffer_get_insert (buffer);
gtk_text_buffer_get_iter_at_mark (buffer, &insert_iter, mark);
gtk_source_buffer_move_cursor (buffer, &insert_iter, mark);
if (source_buffer->priv->highlight_engine != NULL)
_gtk_source_engine_text_inserted (source_buffer->priv->highlight_engine,
start_offset,
end_offset);
}
static void
gtk_source_buffer_real_insert_text (GtkTextBuffer *buffer,
GtkTextIter *iter,
const gchar *text,
gint len)
{
gint start_offset;
g_return_if_fail (GTK_SOURCE_IS_BUFFER (buffer));
g_return_if_fail (iter != NULL);
g_return_if_fail (text != NULL);
g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
start_offset = gtk_text_iter_get_offset (iter);
/*
* iter is invalidated when
* insertion occurs (because the buffer contents change), but the
* default signal handler revalidates it to point to the end of the
* inserted text
*/
GTK_TEXT_BUFFER_CLASS (gtk_source_buffer_parent_class)->insert_text (buffer, iter, text, len);
gtk_source_buffer_content_inserted (buffer,
start_offset,
gtk_text_iter_get_offset (iter));
}
/* insert_pixbuf and insert_child_anchor do nothing except notifying
* the highlighting engine about the change, because engine's idea
* of buffer char count must be correct at all times */
static void
gtk_source_buffer_real_insert_pixbuf (GtkTextBuffer *buffer,
GtkTextIter *iter,
GdkPixbuf *pixbuf)
{
gint start_offset;
g_return_if_fail (GTK_SOURCE_IS_BUFFER (buffer));
g_return_if_fail (iter != NULL);
g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
start_offset = gtk_text_iter_get_offset (iter);
/*
* iter is invalidated when
* insertion occurs (because the buffer contents change), but the
* default signal handler revalidates it to point to the end of the
* inserted text
*/
GTK_TEXT_BUFFER_CLASS (gtk_source_buffer_parent_class)->insert_pixbuf (buffer, iter, pixbuf);
gtk_source_buffer_content_inserted (buffer,
start_offset,
gtk_text_iter_get_offset (iter));
}
static void
gtk_source_buffer_real_insert_anchor (GtkTextBuffer *buffer,
GtkTextIter *iter,
GtkTextChildAnchor *anchor)
{
gint start_offset;
g_return_if_fail (GTK_SOURCE_IS_BUFFER (buffer));
g_return_if_fail (iter != NULL);
g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
start_offset = gtk_text_iter_get_offset (iter);
/*
* iter is invalidated when
* insertion occurs (because the buffer contents change), but the
* default signal handler revalidates it to point to the end of the
* inserted text
*/
GTK_TEXT_BUFFER_CLASS (gtk_source_buffer_parent_class)->insert_child_anchor (buffer, iter, anchor);
gtk_source_buffer_content_inserted (buffer,
start_offset,
gtk_text_iter_get_offset (iter));
}
static void
gtk_source_buffer_real_delete_range (GtkTextBuffer *buffer,
GtkTextIter *start,
GtkTextIter *end)
{
gint offset, length;
GtkTextMark *mark;
GtkTextIter iter;
GtkSourceBuffer *source_buffer = GTK_SOURCE_BUFFER (buffer);
g_return_if_fail (GTK_SOURCE_IS_BUFFER (buffer));
g_return_if_fail (start != NULL);
g_return_if_fail (end != NULL);
g_return_if_fail (gtk_text_iter_get_buffer (start) == buffer);
g_return_if_fail (gtk_text_iter_get_buffer (end) == buffer);
gtk_text_iter_order (start, end);
offset = gtk_text_iter_get_offset (start);
length = gtk_text_iter_get_offset (end) - offset;
GTK_TEXT_BUFFER_CLASS (gtk_source_buffer_parent_class)->delete_range (buffer, start, end);
mark = gtk_text_buffer_get_insert (buffer);
gtk_text_buffer_get_iter_at_mark (buffer, &iter, mark);
gtk_source_buffer_move_cursor (buffer, &iter, mark);
/* emit text deleted for engines */
if (source_buffer->priv->highlight_engine != NULL)
_gtk_source_engine_text_deleted (source_buffer->priv->highlight_engine,
offset, length);
}
/* This describes a mask of relevant context classes for highlighting matching
brackets. Additional classes can be added below */
static const gchar *cclass_mask_definitions[] = {
"comment",
"string",
};
static gint
get_context_class_mask (GtkSourceBuffer *buffer,
GtkTextIter *iter)
{
gint i;
gint ret = 0;
for (i = 0; i < sizeof (cclass_mask_definitions) / sizeof (gchar *); ++i)
{
gboolean hasclass = gtk_source_buffer_iter_has_context_class (buffer,
iter,
cclass_mask_definitions[i]);
ret |= hasclass << i;
}
return ret;
}
static gboolean
gtk_source_buffer_find_bracket_match_real (GtkSourceBuffer *buffer,
GtkTextIter *orig,
GtkSourceBracketMatchType *result,
gint max_chars)
{
GtkTextIter iter;
gunichar base_char;
gunichar search_char;
gunichar cur_char;
gint addition;
gint char_cont;
gint counter;
gboolean found;
gint cclass_mask;
iter = *orig;
cur_char = gtk_text_iter_get_char (&iter);
base_char = cur_char;
cclass_mask = get_context_class_mask (buffer, &iter);
search_char = bracket_pair (base_char, &addition);
if (addition == 0)
{
*result = GTK_SOURCE_BRACKET_MATCH_NONE;
return FALSE;
}
counter = 0;
found = FALSE;
char_cont = 0;
do
{
gint current_mask;
gtk_text_iter_forward_chars (&iter, addition);
cur_char = gtk_text_iter_get_char (&iter);
++char_cont;
current_mask = get_context_class_mask (buffer, &iter);
/* Check if we lost a class, which means we don't look any
further */
if (current_mask < cclass_mask)
{
found = FALSE;
break;
}
if ((cur_char == search_char || cur_char == base_char) &&
cclass_mask == current_mask)
{
if ((cur_char == search_char) && counter == 0)
{
found = TRUE;
break;
}
if (cur_char == base_char)
counter++;
else
counter--;
}
}
while (!gtk_text_iter_is_end (&iter) && !gtk_text_iter_is_start (&iter) &&
((char_cont < max_chars) || (max_chars < 0)));
if (found)
{
*orig = iter;
*result = GTK_SOURCE_BRACKET_MATCH_FOUND;
}
else if (char_cont >= max_chars && max_chars >= 0)
{
*result = GTK_SOURCE_BRACKET_MATCH_OUT_OF_RANGE;
}
else
{
*result = GTK_SOURCE_BRACKET_MATCH_NOT_FOUND;
}
return found;
}
/* Note that we take into account both the character following the cursor and the
* one preceding it. If there are brackets on both sides the one following the
* cursor takes precedence.
*/
static gboolean
gtk_source_buffer_find_bracket_match_with_limit (GtkSourceBuffer *buffer,
GtkTextIter *orig,
GtkSourceBracketMatchType *result,
gint max_chars)
{
GtkTextIter iter;
if (gtk_source_buffer_find_bracket_match_real (buffer, orig, result, max_chars))
{
return TRUE;
}
iter = *orig;
if (!gtk_text_iter_starts_line (&iter) &&
gtk_text_iter_backward_char (&iter))
{
if (gtk_source_buffer_find_bracket_match_real (buffer, &iter, result, max_chars))
{
*orig = iter;
return TRUE;
}
}
return FALSE;
}
/**
* gtk_source_buffer_can_undo:
* @buffer: a #GtkSourceBuffer.
*
* Determines whether a source buffer can undo the last action.
*
* Return value: %TRUE if it's possible to undo the last action.
**/
gboolean
gtk_source_buffer_can_undo (GtkSourceBuffer *buffer)
{
g_return_val_if_fail (GTK_SOURCE_IS_BUFFER (buffer), FALSE);
return gtk_source_undo_manager_can_undo (buffer->priv->undo_manager);
}
/**
* gtk_source_buffer_can_redo:
* @buffer: a #GtkSourceBuffer.
*
* Determines whether a source buffer can redo the last action
* (i.e. if the last operation was an undo).
*
* Return value: %TRUE if a redo is possible.
**/
gboolean
gtk_source_buffer_can_redo (GtkSourceBuffer *buffer)
{
g_return_val_if_fail (GTK_SOURCE_IS_BUFFER (buffer), FALSE);
return gtk_source_undo_manager_can_redo (buffer->priv->undo_manager);
}
/**
* gtk_source_buffer_undo:
* @buffer: a #GtkSourceBuffer.
*
* Undoes the last user action which modified the buffer. Use
* gtk_source_buffer_can_undo() to check whether a call to this
* function will have any effect.
*
* This function emits the #GtkSourceBuffer::undo signal.
**/
void
gtk_source_buffer_undo (GtkSourceBuffer *buffer)
{
g_return_if_fail (GTK_SOURCE_IS_BUFFER (buffer));
g_signal_emit (buffer, buffer_signals[UNDO], 0);
}
/**
* gtk_source_buffer_redo:
* @buffer: a #GtkSourceBuffer.
*
* Redoes the last undo operation. Use gtk_source_buffer_can_redo()
* to check whether a call to this function will have any effect.
*
* This function emits the #GtkSourceBuffer::redo signal.
**/
void
gtk_source_buffer_redo (GtkSourceBuffer *buffer)
{
g_return_if_fail (GTK_SOURCE_IS_BUFFER (buffer));
g_signal_emit (buffer, buffer_signals[REDO], 0);
}
/**
* gtk_source_buffer_get_max_undo_levels:
* @buffer: a #GtkSourceBuffer.
*
* Determines the number of undo levels the buffer will track for
* buffer edits.
*
* Return value: the maximum number of possible undo levels or
* -1 if no limit is set.
**/
gint
gtk_source_buffer_get_max_undo_levels (GtkSourceBuffer *buffer)
{
g_return_val_if_fail (GTK_SOURCE_IS_BUFFER (buffer), 0);
return buffer->priv->max_undo_levels;
}
/**
* gtk_source_buffer_set_max_undo_levels:
* @buffer: a #GtkSourceBuffer.
* @max_undo_levels: the desired maximum number of undo levels.
*
* Sets the number of undo levels for user actions the buffer will
* track. If the number of user actions exceeds the limit set by this
* function, older actions will be discarded.
*
* If @max_undo_levels is -1, no limit is set.
**/
void
gtk_source_buffer_set_max_undo_levels (GtkSourceBuffer *buffer,
gint max_undo_levels)
{
g_return_if_fail (GTK_SOURCE_IS_BUFFER (buffer));
if (buffer->priv->max_undo_levels == max_undo_levels)
{
return;
}
buffer->priv->max_undo_levels = max_undo_levels;
if (GTK_SOURCE_IS_UNDO_MANAGER_DEFAULT (buffer->priv->undo_manager))
{
gtk_source_undo_manager_default_set_max_undo_levels (GTK_SOURCE_UNDO_MANAGER_DEFAULT (buffer->priv->undo_manager),
max_undo_levels);
}
g_object_notify (G_OBJECT (buffer), "max-undo-levels");
}
/**
* gtk_source_buffer_begin_not_undoable_action:
* @buffer: a #GtkSourceBuffer.
*
* Marks the beginning of a not undoable action on the buffer,
* disabling the undo manager. Typically you would call this function
* before initially setting the contents of the buffer (e.g. when
* loading a file in a text editor).
*
* You may nest gtk_source_buffer_begin_not_undoable_action() /
* gtk_source_buffer_end_not_undoable_action() blocks.
**/
void
gtk_source_buffer_begin_not_undoable_action (GtkSourceBuffer *buffer)
{
g_return_if_fail (GTK_SOURCE_IS_BUFFER (buffer));
gtk_source_undo_manager_begin_not_undoable_action (buffer->priv->undo_manager);
}
/**
* gtk_source_buffer_end_not_undoable_action:
* @buffer: a #GtkSourceBuffer.
*
* Marks the end of a not undoable action on the buffer. When the
* last not undoable block is closed through the call to this
* function, the list of undo actions is cleared and the undo manager
* is re-enabled.
**/
void
gtk_source_buffer_end_not_undoable_action (GtkSourceBuffer *buffer)
{
g_return_if_fail (GTK_SOURCE_IS_BUFFER (buffer));
gtk_source_undo_manager_end_not_undoable_action (buffer->priv->undo_manager);
}
/**
* gtk_source_buffer_get_highlight_matching_brackets:
* @buffer: a #GtkSourceBuffer.
*
* Determines whether bracket match highlighting is activated for the
* source buffer.
*
* Return value: %TRUE if the source buffer will highlight matching
* brackets.
**/
gboolean
gtk_source_buffer_get_highlight_matching_brackets (GtkSourceBuffer *buffer)
{
g_return_val_if_fail (GTK_SOURCE_IS_BUFFER (buffer), FALSE);
return (buffer->priv->highlight_brackets != FALSE);
}
/**
* gtk_source_buffer_set_highlight_matching_brackets:
* @buffer: a #GtkSourceBuffer.
* @highlight: %TRUE if you want matching brackets highlighted.
*
* Controls the bracket match highlighting function in the buffer. If
* activated, when you position your cursor over a bracket character
* (a parenthesis, a square bracket, etc.) the matching opening or
* closing bracket character will be highlighted.
**/
void
gtk_source_buffer_set_highlight_matching_brackets (GtkSourceBuffer *buffer,
gboolean highlight)
{
g_return_if_fail (GTK_SOURCE_IS_BUFFER (buffer));
highlight = (highlight != FALSE);
if (highlight != buffer->priv->highlight_brackets)
{
GtkTextIter iter;
GtkTextMark *mark;
buffer->priv->highlight_brackets = highlight;
/* try to see if there is already a bracket match at the
* current position, but only if the tag table is already set
* otherwise we have problems when calling this function
* on init (get_insert creates the tag table as a side effect */
if (buffer->priv->constructed)
{
mark = gtk_text_buffer_get_insert (GTK_TEXT_BUFFER (buffer));
gtk_text_buffer_get_iter_at_mark (GTK_TEXT_BUFFER (buffer), &iter, mark);
gtk_source_buffer_move_cursor (GTK_TEXT_BUFFER (buffer), &iter, mark);
}
g_object_notify (G_OBJECT (buffer), "highlight-matching-brackets");
}
}
/**
* gtk_source_buffer_get_highlight_syntax:
* @buffer: a #GtkSourceBuffer.
*
* Determines whether syntax highlighting is activated in the source
* buffer.
*
* Return value: %TRUE if syntax highlighting is enabled, %FALSE otherwise.
**/
gboolean
gtk_source_buffer_get_highlight_syntax (GtkSourceBuffer *buffer)
{
g_return_val_if_fail (GTK_SOURCE_IS_BUFFER (buffer), FALSE);
return (buffer->priv->highlight_syntax != FALSE);
}
/**
* gtk_source_buffer_set_highlight_syntax:
* @buffer: a #GtkSourceBuffer.
* @highlight: %TRUE to enable syntax highlighting, %FALSE to disable it.
*
* Controls whether syntax is highlighted in the buffer. If @highlight
* is %TRUE, the text will be highlighted according to the syntax
* patterns specified in the language set with
* gtk_source_buffer_set_language(). If @highlight is %FALSE, syntax highlighting
* is disabled and all the GtkTextTag objects that have been added by the
* syntax highlighting engine are removed from the buffer.
**/
void
gtk_source_buffer_set_highlight_syntax (GtkSourceBuffer *buffer,
gboolean highlight)
{
g_return_if_fail (GTK_SOURCE_IS_BUFFER (buffer));
highlight = (highlight != FALSE);
if (buffer->priv->highlight_syntax != highlight)
{
buffer->priv->highlight_syntax = highlight;
g_object_notify (G_OBJECT (buffer), "highlight-syntax");
}
}
/**
* gtk_source_buffer_set_language:
* @buffer: a #GtkSourceBuffer.
* @language: (allow-none): a #GtkSourceLanguage to set, or %NULL.
*
* Associate a #GtkSourceLanguage with the buffer. If @language is
* not-%NULL and syntax highlighting is enabled (see gtk_source_buffer_set_highlight_syntax()),
* the syntax patterns defined in @language will be used to highlight the text
* contained in the buffer. If @language is %NULL, the text contained in the
* buffer is not highlighted.
*
* The buffer holds a reference to @language.
**/
void
gtk_source_buffer_set_language (GtkSourceBuffer *buffer,
GtkSourceLanguage *language)
{
g_return_if_fail (GTK_SOURCE_IS_BUFFER (buffer));
g_return_if_fail (GTK_SOURCE_IS_LANGUAGE (language) || language == NULL);
if (buffer->priv->language == language)
return;
if (buffer->priv->highlight_engine != NULL)
{
/* disconnect the old engine */
_gtk_source_engine_attach_buffer (buffer->priv->highlight_engine, NULL);
g_object_unref (buffer->priv->highlight_engine);
buffer->priv->highlight_engine = NULL;
}
if (buffer->priv->language != NULL)
g_object_unref (buffer->priv->language);
buffer->priv->language = language;
if (language != NULL)
{
g_object_ref (language);
/* get a new engine */
buffer->priv->highlight_engine = _gtk_source_language_create_engine (language);
if (buffer->priv->highlight_engine)
{
_gtk_source_engine_attach_buffer (buffer->priv->highlight_engine,
GTK_TEXT_BUFFER (buffer));
if (buffer->priv->style_scheme)
_gtk_source_engine_set_style_scheme (buffer->priv->highlight_engine,
buffer->priv->style_scheme);
}
}
g_object_notify (G_OBJECT (buffer), "language");
}
/**
* gtk_source_buffer_get_language:
* @buffer: a #GtkSourceBuffer.
*
* Returns the #GtkSourceLanguage associated with the buffer,
* see gtk_source_buffer_set_language(). The returned object should not be
* unreferenced by the user.
*
* Return value: (transfer none): the #GtkSourceLanguage associated with the buffer, or %NULL.
**/
GtkSourceLanguage *
gtk_source_buffer_get_language (GtkSourceBuffer *buffer)
{
g_return_val_if_fail (GTK_SOURCE_IS_BUFFER (buffer), NULL);
return buffer->priv->language;
}
/**
* _gtk_source_buffer_update_highlight:
* @buffer: a #GtkSourceBuffer.
* @start: start of the area to highlight.
* @end: end of the area to highlight.
* @synchronous: whether the area should be highlighted synchronously.
*
* Asks the buffer to analyze and highlight given area.
**/
void
_gtk_source_buffer_update_highlight (GtkSourceBuffer *buffer,
const GtkTextIter *start,
const GtkTextIter *end,
gboolean synchronous)
{
GList *l;
g_return_if_fail (GTK_SOURCE_IS_BUFFER (buffer));
if (buffer->priv->highlight_engine != NULL)
{
_gtk_source_engine_update_highlight (buffer->priv->highlight_engine,
start,
end,
synchronous);
}
for (l = buffer->priv->search_contexts; l != NULL; l = l->next)
{
GtkSourceSearchContext *search_context = l->data;
_gtk_source_search_context_update_highlight (search_context,
start,
end,
synchronous);
}
}
/**
* gtk_source_buffer_ensure_highlight:
* @buffer: a #GtkSourceBuffer.
* @start: start of the area to highlight.
* @end: end of the area to highlight.
*
* Forces buffer to analyze and highlight the given area synchronously.
*
* <note>
* <para>
* This is a potentially slow operation and should be used only
* when you need to make sure that some text not currently
* visible is highlighted, for instance before printing.
* </para>
* </note>
**/
void
gtk_source_buffer_ensure_highlight (GtkSourceBuffer *buffer,
const GtkTextIter *start,
const GtkTextIter *end)
{
_gtk_source_buffer_update_highlight (buffer,
start,
end,
TRUE);
}
/**
* gtk_source_buffer_set_style_scheme:
* @buffer: a #GtkSourceBuffer.
* @scheme: (allow-none): a #GtkSourceStyleScheme or %NULL.
*
* Sets style scheme used by the buffer. If @scheme is %NULL no
* style scheme is used.
**/
void
gtk_source_buffer_set_style_scheme (GtkSourceBuffer *buffer,
GtkSourceStyleScheme *scheme)
{
g_return_if_fail (GTK_SOURCE_IS_BUFFER (buffer));
g_return_if_fail (GTK_SOURCE_IS_STYLE_SCHEME (scheme) || scheme == NULL);
if (buffer->priv->style_scheme == scheme)
return;
if (buffer->priv->style_scheme)
g_object_unref (buffer->priv->style_scheme);
buffer->priv->style_scheme = scheme ? g_object_ref (scheme) : NULL;
update_bracket_match_style (buffer);
if (buffer->priv->highlight_engine != NULL)
_gtk_source_engine_set_style_scheme (buffer->priv->highlight_engine,
scheme);
g_object_notify (G_OBJECT (buffer), "style-scheme");
}
/**
* gtk_source_buffer_get_style_scheme:
* @buffer: a #GtkSourceBuffer.
*
* Returns the #GtkSourceStyleScheme associated with the buffer,
* see gtk_source_buffer_set_style_scheme().
* The returned object should not be unreferenced by the user.
*
* Return value: (transfer none): the #GtkSourceStyleScheme associated
* with the buffer, or %NULL.
**/
GtkSourceStyleScheme *
gtk_source_buffer_get_style_scheme (GtkSourceBuffer *buffer)
{
g_return_val_if_fail (GTK_SOURCE_IS_BUFFER (buffer), NULL);
return buffer->priv->style_scheme;
}
static void
gtk_source_buffer_real_apply_tag (GtkTextBuffer *buffer,
GtkTextTag *tag,
const GtkTextIter *start,
const GtkTextIter *end)
{
GtkSourceBuffer *source;
source = GTK_SOURCE_BUFFER (buffer);
/* We only allow the bracket match tag to be applied when we are doing
it ourselves (i.e. when allow_bracket_match is TRUE). The reason for
doing so is that when you copy/paste from the same buffer, the tags
get pasted too. This is ok for highlighting because the region will
get rehighlighted, but not for bracket matching. */
if (source->priv->allow_bracket_match || tag != get_bracket_match_tag (source))
{
GTK_TEXT_BUFFER_CLASS (gtk_source_buffer_parent_class)->apply_tag (buffer, tag, start, end);
}
}
static void
add_source_mark (GtkSourceBuffer *buffer,
GtkSourceMark *mark)
{
const gchar *category;
GtkSourceMarksSequence *seq;
_gtk_source_marks_sequence_add (buffer->priv->all_source_marks,
GTK_TEXT_MARK (mark));
category = gtk_source_mark_get_category (mark);
seq = g_hash_table_lookup (buffer->priv->source_marks, category);
if (seq == NULL)
{
seq = _gtk_source_marks_sequence_new (GTK_TEXT_BUFFER (buffer));
g_hash_table_insert (buffer->priv->source_marks,
g_strdup (category),
seq);
}
_gtk_source_marks_sequence_add (seq, GTK_TEXT_MARK (mark));
}
static void
gtk_source_buffer_real_mark_set (GtkTextBuffer *buffer,
const GtkTextIter *location,
GtkTextMark *mark)
{
if (GTK_SOURCE_IS_MARK (mark))
{
add_source_mark (GTK_SOURCE_BUFFER (buffer),
GTK_SOURCE_MARK (mark));
g_signal_emit_by_name (buffer, "source_mark_updated", mark);
}
/* if the mark is the insert mark, update bracket matching */
else if (mark == gtk_text_buffer_get_insert (buffer))
{
gtk_source_buffer_move_cursor (buffer, location, mark);
}
GTK_TEXT_BUFFER_CLASS (gtk_source_buffer_parent_class)->mark_set (buffer, location, mark);
}
static void
gtk_source_buffer_real_mark_deleted (GtkTextBuffer *buffer,
GtkTextMark *mark)
{
if (GTK_SOURCE_IS_MARK (mark))
{
GtkSourceBuffer *source_buffer = GTK_SOURCE_BUFFER (buffer);
const gchar *category;
GtkSourceMarksSequence *seq;
category = gtk_source_mark_get_category (GTK_SOURCE_MARK (mark));
seq = g_hash_table_lookup (source_buffer->priv->source_marks, category);
if (_gtk_source_marks_sequence_is_empty (seq))
{
g_hash_table_remove (source_buffer->priv->source_marks, category);
}
g_signal_emit_by_name (buffer, "source_mark_updated", mark);
}
if (GTK_TEXT_BUFFER_CLASS (gtk_source_buffer_parent_class)->mark_deleted != NULL)
{
GTK_TEXT_BUFFER_CLASS (gtk_source_buffer_parent_class)->mark_deleted (buffer, mark);
}
}
static void
gtk_source_buffer_real_undo (GtkSourceBuffer *buffer)
{
g_return_if_fail (gtk_source_undo_manager_can_undo (buffer->priv->undo_manager));
gtk_source_undo_manager_undo (buffer->priv->undo_manager);
}
static void
gtk_source_buffer_real_redo (GtkSourceBuffer *buffer)
{
g_return_if_fail (gtk_source_undo_manager_can_redo (buffer->priv->undo_manager));
gtk_source_undo_manager_redo (buffer->priv->undo_manager);
}
/**
* gtk_source_buffer_create_source_mark:
* @buffer: a #GtkSourceBuffer.
* @name: (allow-none): the name of the mark, or %NULL.
* @category: a string defining the mark category.
* @where: location to place the mark.
*
* Creates a source mark in the @buffer of category @category. A source mark is
* a #GtkTextMark but organised into categories. Depending on the category
* a pixbuf can be specified that will be displayed along the line of the mark.
*
* Like a #GtkTextMark, a #GtkSourceMark can be anonymous if the
* passed @name is %NULL. Also, the buffer owns the marks so you
* shouldn't unreference it.
*
* Marks always have left gravity and are moved to the beginning of
* the line when the user deletes the line they were in.
*
* Typical uses for a source mark are bookmarks, breakpoints, current
* executing instruction indication in a source file, etc..
*
* Return value: (transfer none): a new #GtkSourceMark, owned by the buffer.
*
* Since: 2.2
**/
GtkSourceMark *
gtk_source_buffer_create_source_mark (GtkSourceBuffer *buffer,
const gchar *name,
const gchar *category,
const GtkTextIter *where)
{
GtkSourceMark *mark;
g_return_val_if_fail (GTK_SOURCE_IS_BUFFER (buffer), NULL);
g_return_val_if_fail (category != NULL, NULL);
g_return_val_if_fail (where != NULL, NULL);
mark = gtk_source_mark_new (name, category);
gtk_text_buffer_add_mark (GTK_TEXT_BUFFER (buffer),
GTK_TEXT_MARK (mark),
where);
return mark;
}
static GtkSourceMarksSequence *
get_marks_sequence (GtkSourceBuffer *buffer,
const gchar *category)
{
return category == NULL ?
buffer->priv->all_source_marks :
g_hash_table_lookup (buffer->priv->source_marks, category);
}
GtkSourceMark *
_gtk_source_buffer_source_mark_next (GtkSourceBuffer *buffer,
GtkSourceMark *mark,
const gchar *category)
{
GtkSourceMarksSequence *seq;
GtkTextMark *next_mark;
g_return_val_if_fail (GTK_SOURCE_IS_BUFFER (buffer), NULL);
seq = get_marks_sequence (buffer, category);
if (seq == NULL)
{
return NULL;
}
next_mark = _gtk_source_marks_sequence_next (seq, GTK_TEXT_MARK (mark));
return next_mark == NULL ? NULL : GTK_SOURCE_MARK (next_mark);
}
GtkSourceMark *
_gtk_source_buffer_source_mark_prev (GtkSourceBuffer *buffer,
GtkSourceMark *mark,
const gchar *category)
{
GtkSourceMarksSequence *seq;
GtkTextMark *prev_mark;
g_return_val_if_fail (GTK_SOURCE_IS_BUFFER (buffer), NULL);
seq = get_marks_sequence (buffer, category);
if (seq == NULL)
{
return NULL;
}
prev_mark = _gtk_source_marks_sequence_prev (seq, GTK_TEXT_MARK (mark));
return prev_mark == NULL ? NULL : GTK_SOURCE_MARK (prev_mark);
}
/**
* gtk_source_buffer_forward_iter_to_source_mark:
* @buffer: a #GtkSourceBuffer.
* @iter: an iterator.
* @category: (allow-none): category to search for, or %NULL
*
* Moves @iter to the position of the next #GtkSourceMark of the given
* @category. Returns %TRUE if @iter was moved. If @category is NULL, the
* next source mark can be of any category.
*
* Returns: whether @iter was moved.
*
* Since: 2.2
**/
gboolean
gtk_source_buffer_forward_iter_to_source_mark (GtkSourceBuffer *buffer,
GtkTextIter *iter,
const gchar *category)
{
GtkSourceMarksSequence *seq;
g_return_val_if_fail (GTK_SOURCE_IS_BUFFER (buffer), FALSE);
g_return_val_if_fail (iter != NULL, FALSE);
seq = get_marks_sequence (buffer, category);
if (seq == NULL)
{
return FALSE;
}
return _gtk_source_marks_sequence_forward_iter (seq, iter);
}
/**
* gtk_source_buffer_backward_iter_to_source_mark:
* @buffer: a #GtkSourceBuffer.
* @iter: an iterator.
* @category: (allow-none): category to search for, or %NULL
*
* Moves @iter to the position of the previous #GtkSourceMark of the given
* category. Returns %TRUE if @iter was moved. If @category is NULL, the
* previous source mark can be of any category.
*
* Returns: whether @iter was moved.
*
* Since: 2.2
**/
gboolean
gtk_source_buffer_backward_iter_to_source_mark (GtkSourceBuffer *buffer,
GtkTextIter *iter,
const gchar *category)
{
GtkSourceMarksSequence *seq;
g_return_val_if_fail (GTK_SOURCE_IS_BUFFER (buffer), FALSE);
g_return_val_if_fail (iter != NULL, FALSE);
seq = get_marks_sequence (buffer, category);
if (seq == NULL)
{
return FALSE;
}
return _gtk_source_marks_sequence_backward_iter (seq, iter);
}
/**
* gtk_source_buffer_get_source_marks_at_iter:
* @buffer: a #GtkSourceBuffer.
* @iter: an iterator.
* @category: (allow-none): category to search for, or %NULL
*
* Returns the list of marks of the given category at @iter. If @category
* is %NULL it returns all marks at @iter.
*
* Returns: (element-type GtkSource.Mark) (transfer container):
* a newly allocated #GSList.
*
* Since: 2.2
**/
GSList *
gtk_source_buffer_get_source_marks_at_iter (GtkSourceBuffer *buffer,
GtkTextIter *iter,
const gchar *category)
{
GtkSourceMarksSequence *seq;
g_return_val_if_fail (GTK_SOURCE_IS_BUFFER (buffer), NULL);
g_return_val_if_fail (iter != NULL, NULL);
seq = get_marks_sequence (buffer, category);
if (seq == NULL)
{
return NULL;
}
return _gtk_source_marks_sequence_get_marks_at_iter (seq, iter);
}
/**
* gtk_source_buffer_get_source_marks_at_line:
* @buffer: a #GtkSourceBuffer.
* @line: a line number.
* @category: (allow-none): category to search for, or %NULL
*
* Returns the list of marks of the given category at @line.
* If @category is %NULL, all marks at @line are returned.
*
* Returns: (element-type GtkSource.Mark) (transfer container):
* a newly allocated #GSList.
*
* Since: 2.2
**/
GSList *
gtk_source_buffer_get_source_marks_at_line (GtkSourceBuffer *buffer,
gint line,
const gchar *category)
{
GtkSourceMarksSequence *seq;
GtkTextIter start;
GtkTextIter end;
g_return_val_if_fail (GTK_SOURCE_IS_BUFFER (buffer), NULL);
seq = get_marks_sequence (buffer, category);
if (seq == NULL)
{
return NULL;
}
gtk_text_buffer_get_iter_at_line (GTK_TEXT_BUFFER (buffer),
&start,
line);
end = start;
if (!gtk_text_iter_ends_line (&end))
{
gtk_text_iter_forward_to_line_end (&end);
}
return _gtk_source_marks_sequence_get_marks_in_range (seq, &start, &end);
}
/**
* gtk_source_buffer_remove_source_marks:
* @buffer: a #GtkSourceBuffer.
* @start: a #GtkTextIter.
* @end: a #GtkTextIter.
* @category: (allow-none): category to search for, or %NULL.
*
* Remove all marks of @category between @start and @end from the buffer.
* If @category is NULL, all marks in the range will be removed.
*
* Since: 2.2
**/
void
gtk_source_buffer_remove_source_marks (GtkSourceBuffer *buffer,
const GtkTextIter *start,
const GtkTextIter *end,
const gchar *category)
{
GtkSourceMarksSequence *seq;
GSList *list;
GSList *l;
g_return_if_fail (GTK_SOURCE_IS_BUFFER (buffer));
g_return_if_fail (start != NULL);
g_return_if_fail (end != NULL);
seq = get_marks_sequence (buffer, category);
if (seq == NULL)
{
return;
}
list = _gtk_source_marks_sequence_get_marks_in_range (seq, start, end);
for (l = list; l != NULL; l = l->next)
{
gtk_text_buffer_delete_mark (GTK_TEXT_BUFFER (buffer), l->data);
}
g_slist_free (list);
}
/**
* gtk_source_buffer_iter_has_context_class:
* @buffer: a #GtkSourceBuffer.
* @iter: a #GtkTextIter.
* @context_class: class to search for.
*
* Check if the class @context_class is set on @iter.
*
* See the #GtkSourceBuffer description for the list of default context classes.
*
* Returns: whether @iter has the context class.
* Since: 2.10
**/
gboolean
gtk_source_buffer_iter_has_context_class (GtkSourceBuffer *buffer,
const GtkTextIter *iter,
const gchar *context_class)
{
GtkTextTag *tag;
g_return_val_if_fail (GTK_SOURCE_IS_BUFFER (buffer), FALSE);
g_return_val_if_fail (iter != NULL, FALSE);
g_return_val_if_fail (context_class != NULL, FALSE);
if (buffer->priv->highlight_engine == NULL)
{
return FALSE;
}
tag = _gtk_source_engine_get_context_class_tag (buffer->priv->highlight_engine,
context_class);
if (tag != NULL)
{
return gtk_text_iter_has_tag (iter, tag);
}
else
{
return FALSE;
}
}
/**
* gtk_source_buffer_get_context_classes_at_iter:
* @buffer: a #GtkSourceBuffer.
* @iter: a #GtkTextIter.
*
* Get all defined context classes at @iter.
*
* See the #GtkSourceBuffer description for the list of default context classes.
*
* Returns: (array zero-terminated=1) (transfer full): a new %NULL
* terminated array of context class names.
* Use g_strfreev() to free the array if it is no longer needed.
*
* Since: 2.10
**/
gchar **
gtk_source_buffer_get_context_classes_at_iter (GtkSourceBuffer *buffer,
const GtkTextIter *iter)
{
GSList *tags;
GSList *item;
GPtrArray *ret;
g_return_val_if_fail (GTK_SOURCE_IS_BUFFER (buffer), NULL);
g_return_val_if_fail (iter != NULL, NULL);
tags = gtk_text_iter_get_tags (iter);
ret = g_ptr_array_new ();
for (item = tags; item; item = g_slist_next (item))
{
gchar const *name = g_object_get_data (G_OBJECT (item->data),
TAG_CONTEXT_CLASS_NAME);
if (name != NULL)
{
g_ptr_array_add (ret, g_strdup (name));
}
}
g_slist_free (tags);
g_ptr_array_add (ret, NULL);
return (gchar **) g_ptr_array_free (ret, FALSE);
}
/**
* gtk_source_buffer_iter_forward_to_context_class_toggle:
* @buffer: a #GtkSourceBuffer.
* @iter: (inout): a #GtkTextIter.
* @context_class: the context class.
*
* Moves forward to the next toggle (on or off) of the context class. If no
* matching context class toggles are found, returns %FALSE, otherwise %TRUE.
* Does not return toggles located at @iter, only toggles after @iter. Sets
* @iter to the location of the toggle, or to the end of the buffer if no
* toggle is found.
*
* See the #GtkSourceBuffer description for the list of default context classes.
*
* Returns: whether we found a context class toggle after @iter
*
* Since: 2.10
**/
gboolean
gtk_source_buffer_iter_forward_to_context_class_toggle (GtkSourceBuffer *buffer,
GtkTextIter *iter,
const gchar *context_class)
{
GtkTextTag *tag;
g_return_val_if_fail (GTK_SOURCE_IS_BUFFER (buffer), FALSE);
g_return_val_if_fail (iter != NULL, FALSE);
g_return_val_if_fail (context_class != NULL, FALSE);
if (buffer->priv->highlight_engine == NULL)
{
return FALSE;
}
tag = _gtk_source_engine_get_context_class_tag (buffer->priv->highlight_engine,
context_class);
if (tag == NULL)
{
return FALSE;
}
else
{
return gtk_text_iter_forward_to_tag_toggle (iter, tag);
}
}
/**
* gtk_source_buffer_iter_backward_to_context_class_toggle:
* @buffer: a #GtkSourceBuffer.
* @iter: (inout): a #GtkTextIter.
* @context_class: the context class.
*
* Moves backward to the next toggle (on or off) of the context class. If no
* matching context class toggles are found, returns %FALSE, otherwise %TRUE.
* Does not return toggles located at @iter, only toggles after @iter. Sets
* @iter to the location of the toggle, or to the end of the buffer if no
* toggle is found.
*
* See the #GtkSourceBuffer description for the list of default context classes.
*
* Returns: whether we found a context class toggle before @iter
*
* Since: 2.10
**/
gboolean
gtk_source_buffer_iter_backward_to_context_class_toggle (GtkSourceBuffer *buffer,
GtkTextIter *iter,
const gchar *context_class)
{
GtkTextTag *tag;
g_return_val_if_fail (GTK_SOURCE_IS_BUFFER (buffer), FALSE);
g_return_val_if_fail (iter != NULL, FALSE);
g_return_val_if_fail (context_class != NULL, FALSE);
if (buffer->priv->highlight_engine == NULL)
{
return FALSE;
}
tag = _gtk_source_engine_get_context_class_tag (buffer->priv->highlight_engine,
context_class);
if (tag == NULL)
{
return FALSE;
}
else
{
return gtk_text_iter_backward_to_tag_toggle (iter, tag);
}
}
static gchar *
do_lower_case (GtkTextBuffer *buffer,
const GtkTextIter *start,
const GtkTextIter *end)
{
gchar *text;
gchar *new_text;
text = gtk_text_buffer_get_text (buffer, start, end, TRUE);
new_text = g_utf8_strdown (text, -1);
g_free (text);
return new_text;
}
static gchar *
do_upper_case (GtkTextBuffer *buffer,
const GtkTextIter *start,
const GtkTextIter *end)
{
gchar *text;
gchar *new_text;
text = gtk_text_buffer_get_text (buffer, start, end, TRUE);
new_text = g_utf8_strup (text, -1);
g_free (text);
return new_text;
}
static gchar *
do_toggle_case (GtkTextBuffer *buffer,
const GtkTextIter *start,
const GtkTextIter *end)
{
GString *str;
GtkTextIter iter_start;
str = g_string_new (NULL);
iter_start = *start;
while (!gtk_text_iter_is_end (&iter_start))
{
GtkTextIter iter_end;
gchar *text;
gchar *text_down;
gchar *text_up;
iter_end = iter_start;
gtk_text_iter_forward_cursor_position (&iter_end);
if (gtk_text_iter_compare (end, &iter_end) < 0)
{
break;
}
text = gtk_text_buffer_get_text (buffer, &iter_start, &iter_end, TRUE);
text_down = g_utf8_strdown (text, -1);
text_up = g_utf8_strup (text, -1);
if (g_strcmp0 (text, text_down) == 0)
{
g_string_append (str, text_up);
}
else if (g_strcmp0 (text, text_up) == 0)
{
g_string_append (str, text_down);
}
else
{
g_string_append (str, text);
}
g_free (text);
g_free (text_down);
g_free (text_up);
iter_start = iter_end;
}
return g_string_free (str, FALSE);
}
static gchar *
do_title_case (GtkTextBuffer *buffer,
const GtkTextIter *start,
const GtkTextIter *end)
{
GString *str;
GtkTextIter iter_start;
str = g_string_new (NULL);
iter_start = *start;
while (!gtk_text_iter_is_end (&iter_start))
{
GtkTextIter iter_end;
gchar *text;
iter_end = iter_start;
gtk_text_iter_forward_cursor_position (&iter_end);
if (gtk_text_iter_compare (end, &iter_end) < 0)
{
break;
}
text = gtk_text_buffer_get_text (buffer, &iter_start, &iter_end, TRUE);
if (gtk_text_iter_starts_word (&iter_start))
{
gchar *text_normalized;
text_normalized = g_utf8_normalize (text, -1, G_NORMALIZE_DEFAULT);
if (g_utf8_strlen (text_normalized, -1) == 1)
{
gunichar c;
gunichar new_c;
c = gtk_text_iter_get_char (&iter_start);
new_c = g_unichar_totitle (c);
g_string_append_unichar (str, new_c);
}
else
{
gchar *text_up;
text_up = g_utf8_strup (text, -1);
g_string_append (str, text_up);
g_free (text_up);
}
g_free (text_normalized);
}
else
{
gchar *text_down;
text_down = g_utf8_strdown (text, -1);
g_string_append (str, text_down);
g_free (text_down);
}
g_free (text);
iter_start = iter_end;
}
return g_string_free (str, FALSE);
}
/**
* gtk_source_buffer_change_case:
* @buffer: a #GtkSourceBuffer.
* @case_type: how to change the case.
* @start: a #GtkTextIter.
* @end: a #GtkTextIter.
*
* Changes the case of the text between the specified iterators.
*
* Since: 3.12
**/
void
gtk_source_buffer_change_case (GtkSourceBuffer *buffer,
GtkSourceChangeCaseType case_type,
GtkTextIter *start,
GtkTextIter *end)
{
GtkTextBuffer *text_buffer;
gchar *new_text;
g_return_if_fail (GTK_SOURCE_IS_BUFFER (buffer));
g_return_if_fail (start != NULL);
g_return_if_fail (end != NULL);
gtk_text_iter_order (start, end);
text_buffer = GTK_TEXT_BUFFER (buffer);
switch (case_type)
{
case GTK_SOURCE_CHANGE_CASE_LOWER:
new_text = do_lower_case (text_buffer, start, end);
break;
case GTK_SOURCE_CHANGE_CASE_UPPER:
new_text = do_upper_case (text_buffer, start, end);
break;
case GTK_SOURCE_CHANGE_CASE_TOGGLE:
new_text = do_toggle_case (text_buffer, start, end);
break;
case GTK_SOURCE_CHANGE_CASE_TITLE:
new_text = do_title_case (text_buffer, start, end);
break;
default:
g_return_if_reached ();
}
gtk_text_buffer_begin_user_action (text_buffer);
gtk_text_buffer_delete (text_buffer, start, end);
gtk_text_buffer_insert (text_buffer, start, new_text, -1);
gtk_text_buffer_end_user_action (text_buffer);
g_free (new_text);
}
/**
* gtk_source_buffer_set_undo_manager:
* @buffer: a #GtkSourceBuffer.
* @manager: (allow-none): A #GtkSourceUndoManager or %NULL.
*
* Set the buffer undo manager. If @manager is %NULL the default undo manager
* will be set.
**/
void
gtk_source_buffer_set_undo_manager (GtkSourceBuffer *buffer,
GtkSourceUndoManager *manager)
{
g_return_if_fail (GTK_SOURCE_IS_BUFFER (buffer));
g_return_if_fail (manager == NULL || GTK_SOURCE_IS_UNDO_MANAGER (manager));
if (manager == NULL)
{
manager = g_object_new (GTK_SOURCE_TYPE_UNDO_MANAGER_DEFAULT,
"buffer", buffer,
"max-undo-levels", buffer->priv->max_undo_levels,
NULL);
}
else
{
g_object_ref (manager);
}
set_undo_manager (buffer, manager);
g_object_unref (manager);
g_object_notify (G_OBJECT (buffer), "undo-manager");
}
/**
* gtk_source_buffer_get_undo_manager:
* @buffer: a #GtkSourceBuffer.
*
* Returns the #GtkSourceUndoManager associated with the buffer,
* see gtk_source_buffer_set_undo_manager(). The returned object should not be
* unreferenced by the user.
*
* Returns: (nullable) (transfer none): the #GtkSourceUndoManager associated
* with the buffer, or %NULL.
**/
GtkSourceUndoManager *
gtk_source_buffer_get_undo_manager (GtkSourceBuffer *buffer)
{
g_return_val_if_fail (GTK_SOURCE_IS_BUFFER (buffer), NULL);
return buffer->priv->undo_manager;
}
void
_gtk_source_buffer_add_search_context (GtkSourceBuffer *buffer,
GtkSourceSearchContext *search_context)
{
g_return_if_fail (GTK_SOURCE_IS_BUFFER (buffer));
g_return_if_fail (GTK_SOURCE_IS_SEARCH_CONTEXT (search_context));
g_return_if_fail (gtk_source_search_context_get_buffer (search_context) == buffer);
if (g_list_find (buffer->priv->search_contexts, search_context) != NULL)
{
return;
}
buffer->priv->search_contexts = g_list_prepend (buffer->priv->search_contexts,
search_context);
g_object_weak_ref (G_OBJECT (search_context),
(GWeakNotify)search_context_weak_notify_cb,
buffer);
}
static void
sync_invalid_char_tag (GtkSourceBuffer *buffer,
GParamSpec *pspec,
gpointer data)
{
GtkSourceStyle *style = NULL;
if (buffer->priv->style_scheme != NULL)
{
style = gtk_source_style_scheme_get_style (buffer->priv->style_scheme, "def:error");
}
_gtk_source_style_apply (style, buffer->priv->invalid_char_tag);
}
static void
text_tag_set_highest_priority (GtkTextTag *tag,
GtkTextBuffer *buffer)
{
GtkTextTagTable *table;
gint n;
table = gtk_text_buffer_get_tag_table (buffer);
n = gtk_text_tag_table_get_size (table);
gtk_text_tag_set_priority (tag, n - 1);
}
void
_gtk_source_buffer_set_as_invalid_character (GtkSourceBuffer *buffer,
const GtkTextIter *start,
const GtkTextIter *end)
{
if (buffer->priv->invalid_char_tag == NULL)
{
buffer->priv->invalid_char_tag = gtk_text_buffer_create_tag (GTK_TEXT_BUFFER (buffer),
"invalid-char-style",
NULL);
sync_invalid_char_tag (buffer, NULL, NULL);
g_signal_connect (buffer,
"notify::style-scheme",
G_CALLBACK (sync_invalid_char_tag),
NULL);
}
/* Make sure the 'error' tag has the priority over
* syntax highlighting tags.
*/
text_tag_set_highest_priority (buffer->priv->invalid_char_tag,
GTK_TEXT_BUFFER (buffer));
gtk_text_buffer_apply_tag (GTK_TEXT_BUFFER (buffer),
buffer->priv->invalid_char_tag,
start,
end);
}
gboolean
_gtk_source_buffer_has_invalid_chars (GtkSourceBuffer *buffer)
{
GtkTextIter start;
g_return_val_if_fail (GTK_SOURCE_IS_BUFFER (buffer), FALSE);
if (buffer->priv->invalid_char_tag == NULL)
{
return FALSE;
}
gtk_text_buffer_get_start_iter (GTK_TEXT_BUFFER (buffer), &start);
if (gtk_text_iter_begins_tag (&start, buffer->priv->invalid_char_tag) ||
gtk_text_iter_forward_to_tag_toggle (&start, buffer->priv->invalid_char_tag))
{
return TRUE;
}
return FALSE;
}
/**
* gtk_source_buffer_set_implicit_trailing_newline:
* @buffer: a #GtkSourceBuffer.
* @implicit_trailing_newline: the new value.
*
* Sets whether the @buffer has an implicit trailing newline.
*
* If an explicit trailing newline is present in a #GtkTextBuffer, #GtkTextView
* shows it as an empty line. This is generally not what the user expects.
*
* If @implicit_trailing_newline is %TRUE (the default value):
* - when a #GtkSourceFileLoader loads the content of a file into the @buffer,
* the trailing newline (if present in the file) is not inserted into the
* @buffer.
* - when a #GtkSourceFileSaver saves the content of the @buffer into a file, a
* trailing newline is added to the file.
*
* On the other hand, if @implicit_trailing_newline is %FALSE, the file's
* content is not modified when loaded into the @buffer, and the @buffer's
* content is not modified when saved into a file.
*
* Since: 3.14
*/
void
gtk_source_buffer_set_implicit_trailing_newline (GtkSourceBuffer *buffer,
gboolean implicit_trailing_newline)
{
g_return_if_fail (GTK_SOURCE_IS_BUFFER (buffer));
implicit_trailing_newline = implicit_trailing_newline != FALSE;
if (buffer->priv->implicit_trailing_newline != implicit_trailing_newline)
{
buffer->priv->implicit_trailing_newline = implicit_trailing_newline;
g_object_notify (G_OBJECT (buffer), "implicit-trailing-newline");
}
}
/**
* gtk_source_buffer_get_implicit_trailing_newline:
* @buffer: a #GtkSourceBuffer.
*
* Returns: whether the @buffer has an implicit trailing newline.
* Since: 3.14
*/
gboolean
gtk_source_buffer_get_implicit_trailing_newline (GtkSourceBuffer *buffer)
{
g_return_val_if_fail (GTK_SOURCE_IS_BUFFER (buffer), TRUE);
return buffer->priv->implicit_trailing_newline;
}
|