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
|
/***********************************************************************
*
* Module: textbuf.c
* Author: Marc van Kempen (marc@bowtie.nl)
* Desc: Textbuffer manipulation routines. Implementation of a
* texteditor
*
* Copyright (c) 1996, Marc van Kempen
*
* All rights reserved.
*
* This software may be used, modified, copied, distributed, and
* sold, in both source and binary form provided that the above
* copyright and these terms are retained, verbatim, as the first
* lines of this file. Under no circumstances is the author
* responsible for the proper functioning of this software, nor does
* the author assume any responsibility for damages incurred with
* its use.
*
***********************************************************************/
/*
* Copyright(c) 1997-1998 by Gennady B. Sorokopud (gena@NetVision.net.il)
*
* This software can be freely redistributed and modified for
* non-commercial purposes as long as above copyright
* message and this permission notice appear in all
* copies of distributed source code and included as separate file
* in binary distribution.
*
* Any commercial use of this software requires author's permission.
*
* This software is provided "as is" without expressed or implied
* warranty of any kind.
* Under no circumstances is the author responsible for the proper
* functioning of this software, nor does the author assume any
* responsibility for damages incurred with its use.
*
*/
/* $Id: textbuf.c,v 1.3 1999/11/07 22:27:53 xfmail Exp $
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include "textbuf.h"
#include "fl_error.h"
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
#define TB_BUFINC 80 /* to be added space to an existing buffer */
#define TB_TABLEN 8 /* default tablen */
#define tb_error fl_edit_error
/***********************************************************************
*
* Textbuffer manipulation functions
*
***********************************************************************/
void tb_init(TextBuf * tb)
/*
* Desc: initialize the textbuffer structure
* Pre: <tb> is a pointer to an existing TextBuf structure
* Post: <tb> has been initialized
*/ {
tb->firstline = NULL;
tb->currentline = NULL;
tb->lastline = NULL;
tb->n = 0;
tb->i = 0;
tb->bufchanged = FALSE;
tb->tablen = TB_TABLEN;
tb->bgcolor_def = 0;
tb->fgcolor_def = 1;
tb->linewrap = 0;
tb->flags = 0;
tb->maxchars = 0;
tb->attr_def = ATTR_EMPTY;
return;
} /* tb_init() */
void tb_clear(TextBuf * tb)
/*
* Desc: release the textbuffer, free the used memory by the TextLine
* structures.
*/ {
TextLine *tl, *tlold;
/* free the memory */
tl = tb->firstline;
while(tl) {
tlold = tl;
if(tl->buf)
free(tl->buf);
if(tl->attr)
free(tl->attr);
tl = tl->next;
free(tlold);
}
/* initialize the structure */
tb->firstline = NULL;
tb->currentline = NULL;
tb->lastline = NULL;
tb->n = 0;
tb->i = 0;
tb->bufchanged = FALSE;
tb->maxchars = 0;
return;
} /* tb_clear() */
/***********************************************************************
*
* Retrieval
*
***********************************************************************/
int tb_get_char(TextBuf * tb, int n, char *ch)
/*
* Desc: retrieve a single character from the n-th position in the
* current line.
* Pre: ch is a pointer to a char variable,
n < strlen(currentline)
* Post: *ch contains the retrieved value
*/ {
if(n < tb->currentline->strlen) {
*ch = tb->currentline->buf[n];
return(TRUE);
} else {
return(FALSE);
}
} /* tb_get_char() */
void tb_get_line(TextBuf * tb, char **line)
/*
* Desc: retrieve the current line
* Pre: *line is a pointer to a character buffer, not allocated
* Post: *line points to the line requested
*/ {
if(tb->currentline) {
*line = tb->currentline->buf;
} else {
*line = NULL;
}
return;
} /* tb_get_line() */
char *tb_return_line(TextBuf * tb)
/*
* Desc: returns pointer to the text of current line
*/ {
return tb->currentline ? tb->currentline->buf : NULL;
} /* tb_return_line() */
TextLine *tb_get_lineptr_by_num(TextBuf * tb, int n)
/*
* Desc: returns pointer to the line with number <n>
*/ {
int i;
TextLine *tl;
i = 0;
tl = tb->firstline;
while(tl && i < n) {
tl = tl->next;
i++;
}
if(tl && i == n) {
/* we've got the right line */
return tl;
} else {
/* the line does not exist */
return NULL;
}
return NULL;
}
void tb_get_line_by_num(TextBuf * tb, char **line, int n)
/*
* Desc: retrieve the line with number <n>
* Pre: *line is a pointer to a character buffer, not allocated
* Post: *line points to the line requested
*/ {
int i;
TextLine *tl;
i = 0;
tl = tb->firstline;
while(tl && i < n) {
tl = tl->next;
i++;
}
if(tl && i == n) {
/* we've got the right line */
*line = tl->buf;
return;
} else {
/* the line does not exist */
*line = NULL;
return;
}
}
void
tb_set_block_attr(TextBuf * tb, int r0, int c0, int r1, int c1, int attr)
/*
* Desc: set attrbutes of specific block
*/ {
int i, k, rtop, rbottom;
struct TextLine *tl;
if(r0 > r1) {
rtop = r0;
rbottom = r1;
} else {
rtop = r1;
rbottom = r0;
}
if(rtop == rbottom) {
if((tl = tb_get_lineptr_by_num(tb, rtop)) == NULL)
return;
if(c0 < 0)
c0 = tl->strlen;
if(c1 < 0)
c1 = tl->strlen;
if(c1 < c0) {
i = c0;
c0 = c1;
c1 = i;
}
if(c1 > tl->strlen)
c1 = tl->strlen;
if(c0 > tl->strlen)
c0 = tl->strlen;
if(c0 == c1)
return;
for(i = c0; i < c1; i++)
tl->attr[i] = attr;
tl->flags |= TLINE_MODIFIED;
return;
}
for(i = rbottom; i <= rtop; i++) {
if((tl = tb_get_lineptr_by_num(tb, i)) == NULL)
continue;
if(i == rbottom) {
if(c0 >= tl->strlen)
continue;
for(k = c0; k < tl->strlen; k++)
tl->attr[k] = attr;
} else if(i == rtop) {
if((c1 >= tl->strlen) || (c1 < 0))
c1 = tl->strlen;
for(k = 0; k < c1; k++)
tl->attr[k] = attr;
} else {
for(k = 0; k < tl->strlen; k++)
tl->attr[k] = attr;
}
tl->flags |= TLINE_MODIFIED;
}
return;
}
void tb_get_block(TextBuf * tb, int r0, int c0, int r1, int c1, char **buf)
/*
* Desc: retrieve a block of characters from the TextBuf <tb> and copy
* it into a to be allocated buffer <buf>
*/ {
int i, addcr, rtop, rbottom, buflen, llen;
char *block, *line;
if(r0 > r1) {
rtop = r0;
rbottom = r1;
} else {
rtop = r1;
rbottom = r0;
}
*buf = NULL;
block = (char *) malloc(1);
block[0] = '\0';
buflen = 1;
addcr = 0;
if(rtop == rbottom) {
tb_get_line_by_num(tb, &line, rtop);
if(line == NULL)
return;
llen = strlen(line);
if(c0 < 0)
c0 = llen;
if(c1 < 0)
c1 = llen;
if(c1 < c0) {
i = c0;
c0 = c1;
c1 = i;
}
if(c1 > llen)
c1 = llen;
if(c0 > llen)
c0 = llen;
if(c0 == c1)
return;
buflen += (c1 - c0);
if(c0 == 0) {
addcr = 1;
buflen++;
}
block = (char *) realloc(block, buflen);
strncat(block, line + c0, c1 - c0);
if(addcr)
strcat(block, "\n");
*buf = block;
return;
}
for(i = rbottom; i <= rtop; i++) {
tb_get_line_by_num(tb, &line, i);
if(line == NULL)
continue;
if(i == rbottom) {
if(c0 >= strlen(line))
continue;
buflen += strlen(line) - c0;
if(i < rtop)
buflen++;
block = (char *) realloc(block, buflen);
strcat(block, line + c0);
if(i < rtop)
strcat(block, "\n");
} else if(i == rtop) {
if((c1 >= strlen(line)) || (c1 < 0)) {
c1 = strlen(line) + 1;
addcr = 1;
}
buflen += c1;
block = (char *) realloc(block, buflen);
strncat(block, line, c1);
if(addcr)
strcat(block, "\n");
} else {
buflen += (strlen(line) + 1);
block = (char *) realloc(block, buflen);
strcat(block, line);
strcat(block, "\n");
}
}
*buf = block;
return;
} /* tb_get() */
void tb_get_rect(TextBuf * tb, int r0, int c0, int r1, int c1, char **buf)
/*
* Desc: retrieve a rectangular block of characters from the TextBuf <tb>
* and copy it into a newly allocated buffer <buf>
*/ {
return;
} /* tb_get_rect() */
/***********************************************************************
*
* Insertion
*
***********************************************************************/
int tb_insert_char(TextBuf * tb, int n, char ch)
/*
* Desc: insert the character <ch> into <tb> at the n-th position
* in the current line.
* returns number of characters added or minus this number if the line
* was wrapped
*/ {
TextLine *tl;
int i, added;
char *newbuf;
/* get the current TextLine */
tl = tb->currentline;
/* check if there is a current line at all */
if(!tb->currentline) {
/* allocate a line */
tb_insert_line(tb, "");
tl = tb->currentline;
}
/* check if the character can be fitted in the buffer */
tl->flags |= TLINE_MODIFIED;
if(tl->strlen + 1 >= tl->buflen) {
/* reallocate the buffer space and add a fixed amount of space
to it */
newbuf = (char *) realloc(tl->buf, tl->buflen + TB_BUFINC);
if(!newbuf) {
tb_error
("tb_insert_char(): Could not realloc, character not inserted");
return 0;
}
tl->buf = newbuf;
newbuf = (char *) realloc(tl->attr, tl->buflen + TB_BUFINC);
if(!newbuf) {
tb_error
("tb_insert_char(): Could not realloc attr, character not inserted");
return 0;
}
tl->attr = newbuf;
tl->buflen = tl->buflen + TB_BUFINC;
}
/* first move the charachter after n one position */
for(i = tl->strlen; i >= n; i--) {
tl->buf[i + 1] = tl->buf[i];
tl->attr[i + 1] = tl->attr[i];
}
/* now insert the character */
tl->buf[n] = ch;
tl->attr[n] = tb->attr_def;
/* adjust strlen */
tl->strlen++;
tb->bufchanged = TRUE;
if(ch == '\t')
added = tb_handle_tabs(tb);
else
added = 1;
i = tb_reformat(tb);
tb_fix_line(tl);
return i ? -1 * added : added;
} /* tb_insert_char() */
void tb_insert_line(TextBuf * tb, char *line)
/*
* Desc: insert the line <line> into <tb>
*/ {
TextLine *tlnew;
int i;
/* Allocate a new TextLine buffer */
tlnew = (TextLine *) malloc(sizeof(TextLine));
if(!tlnew) {
tb_error("tb_insert_line(): could not malloc textline");
return;
}
/* allocate a new line buffer and copy the line into it */
tlnew->buf = (char *) malloc(strlen(line) + 1);
if(!tlnew->buf) {
tb_error("tb_insert_line(): could not malloc line buffer");
free(tlnew);
return;
}
strcpy(tlnew->buf, line);
tlnew->strlen = strlen(line);
tlnew->attr = (char *) malloc(tlnew->strlen + 1);
if(!tlnew->attr) {
tb_error("tb_insert_line(): could not malloc line attr buffer");
free(tlnew->buf);
free(tlnew);
return;
}
for(i = 0; i < tlnew->strlen; i++)
tlnew->attr[i] = tb->attr_def;
tlnew->attr[tlnew->strlen] = '\0';
tlnew->buflen = strlen(line) + 1;
tlnew->bgcolor = tb->bgcolor_def;
tlnew->fgcolor = tb->fgcolor_def;
tlnew->cont = NULL;
tlnew->flags = TLINE_MODIFIED;
tlnew->clk_callback = NULL;
tlnew->dbl_callback = NULL;
tlnew->cur_callback = NULL;
tlnew->callback_data = 0;
/* check if there are any lines */
if(tb->currentline) {
/* insert the new TextLine in the linked list */
tlnew->prev = tb->currentline->prev;
tlnew->next = tb->currentline;
if(tb->currentline->prev)
tb->currentline->prev->next = tlnew;
else
tb->firstline = tlnew;
tb->currentline->prev = tlnew;
tb->currentline = tlnew;
} else {
tlnew->prev = NULL;
tlnew->next = NULL;
tb->firstline = tlnew;
tb->currentline = tlnew;
tb->lastline = tlnew;
}
if(tlnew->prev)
tlnew->prev->cont = NULL;
tb_fix_line(tlnew);
while(tlnew) {
tlnew->flags |= TLINE_MODIFIED;
tlnew = tlnew->next;
}
tb->n++;
tb->bufchanged = TRUE;
tb_handle_tabs(tb);
tb_reformat(tb);
return;
} /* tb_insert_line() */
void tb_insert_block(TextBuf * tb, int r, int c, char *buf)
/*
* Desc: insert the buffer <buf> into <tb> at the position (r,c)
*/ {
TextLine *tl, *tlold;
int i, len, oneline, oldwrap;
char *newbuf, *p;
/* store the current line */
tlold = tb->currentline;
/* no lines yet. insert as a new line(s) */
if((tlold == NULL) && (r == 0)) {
/* insert one line */
if((p = strchr(buf, '\n')) == NULL)
tb_insert_line(tb, buf);
else {
/* append multiple lines */
do {
tb_append_buf(tb, buf, p - buf);
buf = ++p;
} while((p = strchr(buf, '\n')) != NULL);
tb_append_line(tb, buf);
}
tb->bufchanged = TRUE;
return;
}
/* check if line <r> exists */
if(tb_set_current_line(tb, r) == FALSE)
return;
/* check if position is valid */
if((c > tb->currentline->strlen) || (c < 0))
c = tb->currentline->strlen;
tl = tb->currentline;
tl->flags |= TLINE_MODIFIED;
/* check if we inserting one-line buffer */
if((p = strchr(buf, '\n')) == NULL) {
len = strlen(buf);
oneline = 1;
} else {
len = p - buf;
oneline = 0;
}
/* check if the buffer can be fitted in string's buffer */
if(tl->strlen + len >= tl->buflen) {
/* reallocate the buffer space and add needed amount of space to it */
newbuf = (char *) realloc(tl->buf, tl->buflen + len + 1);
if(!newbuf) {
tb_error
("tb_insert_buf(): Could not realloc, character not inserted");
tb->currentline = tlold;
return;
}
tl->buf = newbuf;
newbuf = (char *) realloc(tl->attr, tl->buflen + len + 1);
if(!newbuf) {
tb_error
("tb_insert_buf(): Could not realloc attr, character not inserted");
tb->currentline = tlold;
return;
}
tl->attr = newbuf;
tl->buflen = tl->buflen + len + 1;
}
/* store part of the string after <c> */
newbuf = strdup(tl->buf + c);
tl->buf[c] = '\0';
/* add buf */
strncat(tl->buf, buf, len);
/* add remaining part of the string */
strcat(tl->buf, newbuf);
tl->strlen += len;
free(newbuf);
newbuf = strdup(tl->attr + c);
tl->attr[c] = '\0';
for(i = c; i < len + c; i++)
tl->attr[i] = tb->attr_def;
tl->attr[len + c] = '\0';
strcat(tl->attr, newbuf);
free(newbuf);
tb_handle_tabs(tb);
if(oneline) {
/* wrap the line if needed */
tb_reformat(tb);
/* restore currentline */
tb->currentline = tlold;
tb->bufchanged = TRUE;
return;
}
/* insert newline at the end of inserted text */
tb_insert_cr(tb, c + len);
/* start inserting new lines at the next line */
tb_set_next_line(tb);
/* temporary disable line/word wrapping */
oldwrap = tb->linewrap;
tb->linewrap = 0;
/* insert multiple lines */
buf = ++p;
while((p = strchr(buf, '\n')) != NULL) {
len = p - buf;
newbuf = (char *) malloc(len + 1);
strncpy(newbuf, buf, len);
newbuf[len] = '\0';
tb_insert_line(tb, newbuf);
free(newbuf);
tb_set_next_line(tb);
buf = ++p;
}
/* the last line should be inserted at the beginning of the next one */
tl = tb->currentline;
tl->flags |= TLINE_MODIFIED;
if(*buf && tl) {
len = strlen(buf);
/* check if the buffer can be fitted in string's buffer */
if(tl->strlen + len >= tl->buflen) {
/* reallocate the buffer space and add needed amount of space to it */
newbuf = (char *) realloc(tl->buf, tl->buflen + len + 1);
if(!newbuf) {
tb_error
("tb_insert_buf(): Could not realloc, character not inserted");
tb->currentline = tlold;
return;
}
tl->buf = newbuf;
newbuf = (char *) realloc(tl->attr, tl->buflen + len + 1);
if(!newbuf) {
tb_error
("tb_insert_buf(): Could not realloc attr, character not inserted");
tb->currentline = tlold;
return;
}
tl->attr = newbuf;
tl->buflen = tl->buflen + len + 1;
}
tl->strlen += len;
/* store part of the string after <c> */
newbuf = strdup(tl->buf);
/* add buf */
strcpy(tl->buf, buf);
/* add remaining part of the string */
strcat(tl->buf, newbuf);
free(newbuf);
newbuf = strdup(tl->attr);
for(i = 0; i < len; i++)
tl->attr[i] = tb->attr_def;
tl->attr[len] = '\0';
strcat(tl->attr, newbuf);
free(newbuf);
tb_handle_tabs(tb);
}
/* restore wrap value */
tb->linewrap = oldwrap;
/* wrap the whole buffer if needed */
tb_wrap_lines(tb);
tb_reformat(tb);
/* restore currentline */
tb->currentline = tlold;
tb->bufchanged = TRUE;
return;
} /* tb_insert() */
void tb_insert_rect(TextBuf * tb, int r, int c, int w, int h, char *buf)
/*
* Desc: insert the block <buf> into <tb> at the position (r,c)
* Pre: <buf> has dimensions (w,h)
*/ {
tb->bufchanged = TRUE;
return;
} /* tb_insert_rect() */
void tb_insert_cr(TextBuf * tb, int c)
/*
* Desc: at position <c> in the current line insert a newline character
* and move the remainder of the line one line down.
*/ {
char *newline;
TextLine *tl;
if(!tb->currentline) {
/* we must have a virgin textbuffer!, just append two lines */
tb_append_line(tb, "");
tb_append_line(tb, "");
return;
}
/* check to see if c is valid */
if((c < 0) || (c > tb->currentline->strlen))
return;
if(c < tb->currentline->strlen) {
/* get the line from the c-th position */
newline = strdup(tb->currentline->buf + c);
if(!newline) {
tb_error
("tb_insert_newline(): Could not allocate space for newline");
return;
}
tb->currentline->buf[c] = '\0';
tb->currentline->strlen = c;
tb->currentline->flags |= TLINE_MODIFIED;
if(tb->currentline == tb->lastline)
tb_append_line(tb, newline);
else {
/* insert a line at the next line */
tb_set_next_line(tb);
tb_insert_line(tb, newline);
tb_set_prev_line(tb);
}
free(newline);
newline = strdup(tb->currentline->attr + c);
if(!newline) {
tb_error
("tb_insert_newline(): Could not allocate space for newline attr");
return;
}
tb->currentline->attr[c] = '\0';
if(tb->currentline->next)
strcpy(tb->currentline->next->attr, newline);
free(newline);
} else {
/* we are at the end of the line, c == tb->currentline->strlen */
if(tb->currentline == tb->lastline) {
tb_append_line(tb, "");
} else {
tb_set_next_line(tb);
tb_insert_line(tb, "");
tb_set_prev_line(tb);
}
}
tb->currentline->cont = NULL;
tl = tb->currentline;
while(tl) {
tl->flags |= TLINE_MODIFIED;
tl = tl->next;
}
return;
} /* tb_insert_newline() */
/***********************************************************************
*
* Appending
*
***********************************************************************/
void tb_append_line(TextBuf * tb, char *line)
/*
* Desc: Append the <line> to <tb>
*/ {
TextLine *tlnew, *tlcur;
int i;
/* allocate a new TextLine */
tlnew = (TextLine *) malloc(sizeof(TextLine));
if(!tlnew) {
tb_error("tb_append_line(): Could not allocate TextLine");
return;
}
/* Allocate the buffer in TextLine and copy the line into it */
tlnew->buf = strdup(line ? line : "");
if(!tlnew->buf) {
tb_error("tb_append_line(): Could not allocate line buffer");
free(tlnew);
return;
}
tlnew->attr = strdup(line ? line : "");
if(!tlnew->attr) {
tb_error("tb_append_line(): Could not allocate line attr buffer");
free(tlnew->buf);
free(tlnew);
return;
}
tlnew->strlen = strlen(tlnew->buf);
for(i = 0; i < tlnew->strlen; i++)
tlnew->attr[i] = tb->attr_def;
tlnew->buflen = tlnew->strlen + 1;
tlnew->bgcolor = tb->bgcolor_def;
tlnew->fgcolor = tb->fgcolor_def;
tlnew->cont = NULL;
tlnew->flags = TLINE_MODIFIED;
tlnew->clk_callback = NULL;
tlnew->dbl_callback = NULL;
tlnew->cur_callback = NULL;
tlnew->callback_data = 0;
tb_fix_line(tlnew);
/* check if there are any lines at all yet */
if(tb->lastline) {
tlnew->next = NULL;
tlnew->prev = tb->lastline;
tb->lastline->next = tlnew;
tb->lastline = tlnew;
} else {
tlnew->next = NULL;
tlnew->prev = NULL;
tb->firstline = tlnew;
tb->currentline = tlnew;
tb->lastline = tlnew;
}
tb->n++;
tb->bufchanged = TRUE;
if(tlnew->prev)
tlnew->prev->cont = NULL;
tlcur = tb->currentline;
tb->currentline = tlnew;
tb_handle_tabs(tb);
tb_reformat(tb);
tb->currentline = tlcur;
return;
} /* tb_append_line() */
void tb_append_buf(TextBuf * tb, char *buf, u_long len)
/*
* Desc: Append the <buf> with length <len> to <tb>
*/ {
TextLine *tlnew, *tlcur;
int i;
/* allocate a new TextLine */
tlnew = (TextLine *) malloc(sizeof(TextLine));
if(!tlnew) {
tb_error("tb_append_buf(): Could not allocate TextLine");
return;
}
/* Allocate the buffer in TextLine and copy the line into it */
tlnew->buf = (char *) malloc(len + 1);
if(!tlnew->buf) {
tb_error("tb_append_buf(): Could not allocate line buffer");
free(tlnew);
return;
}
tlnew->attr = (char *) malloc(len + 1);
if(!tlnew->attr) {
tb_error("tb_append_buf(): Could not allocate line attr buffer");
free(tlnew->buf);
free(tlnew);
return;
}
for(i = 0; i < len; i++)
tlnew->attr[i] = tb->attr_def;
strncpy(tlnew->buf, buf, len);
tlnew->buf[len] = '\0';
tlnew->attr[len] = '\0';
tlnew->buflen = len + 1;
tlnew->strlen = len;
tlnew->bgcolor = tb->bgcolor_def;
tlnew->fgcolor = tb->fgcolor_def;
tlnew->cont = NULL;
tlnew->flags = TLINE_MODIFIED;
tlnew->clk_callback = NULL;
tlnew->dbl_callback = NULL;
tlnew->cur_callback = NULL;
tlnew->callback_data = 0;
tb_fix_line(tlnew);
/* check if there are any lines at all yet */
if(tb->lastline) {
tlnew->next = NULL;
tlnew->prev = tb->lastline;
tb->lastline->next = tlnew;
tb->lastline = tlnew;
} else {
tlnew->next = NULL;
tlnew->prev = NULL;
tb->firstline = tlnew;
tb->currentline = tlnew;
tb->lastline = tlnew;
}
tb->n++;
tb->bufchanged = TRUE;
if(tlnew->prev)
tlnew->prev->cont = NULL;
tlcur = tb->currentline;
tb->currentline = tlnew;
tb_handle_tabs(tb);
tb_reformat(tb);
tb->currentline = tlcur;
return;
} /* tb_append_buf() */
void tb_append_to_line(TextBuf * tb, char *line)
/*
* Desc: Append the <line> to the current line
*/ {
TextLine *tl, *tlcur;
char *newbuf;
int incr, i;
if(!tb->currentline)
return;
tl = tb->currentline;
tl->flags |= TLINE_MODIFIED;
if(tl->strlen + strlen(line) + 1 >= tl->buflen) {
/* reallocate the buffer space and add needed amount of space to it */
incr =
(strlen(line) + 1 >
TB_BUFINC) ? (strlen(line) + 1) : TB_BUFINC;
newbuf = (char *) realloc(tl->buf, tl->buflen + incr);
if(!newbuf) {
tb_error
("tb_append_to_line(): Could not realloc, buffer not appended");
return;
}
tl->buf = newbuf;
newbuf = (char *) realloc(tl->attr, tl->buflen + incr);
if(!newbuf) {
tb_error
("tb_append_to_line(): Could not realloc attr, buffer not appended");
return;
}
tl->attr = newbuf;
tl->buflen += incr;
}
for(i = tl->strlen; i < tl->strlen + strlen(line); i++)
tl->attr[i] = '\0';
strcat(tl->buf, line);
tl->strlen = strlen(tl->buf);
tl->attr[tl->strlen] = '\0';
tlcur = tb->currentline;
tb_fix_line(tl);
tb_handle_tabs(tb);
tb_reformat(tb);
tb->currentline = tlcur;
return;
}
int tb_wrap_line(TextBuf * tb)
/*
* Desc: wrap current line if needed
*/ {
char *p, *nextline, *nextattr, *newbuf, c;
TextLine *tlwrap;
int nlen, i, linewrap = tb->linewrap;
tlwrap = tb->currentline;
if(!tlwrap || (linewrap == 0)) { /* is wrapping on? */
if(tlwrap->strlen > tb->maxchars)
tb->maxchars = tlwrap->strlen;
return 0;
}
/* check if line should be wrapped */
if(tlwrap->strlen <= abs(linewrap)) {
if(tlwrap->strlen > tb->maxchars)
tb->maxchars = tlwrap->strlen;
return 0;
}
tlwrap->strlen = abs(linewrap);
nextline = tlwrap->buf + tlwrap->strlen;
if(linewrap < 0) { /* word wrap */
c = *nextline;
*nextline = '\0';
if((p = strrchr(tlwrap->buf, ' ')) != NULL) {
*nextline = c;
tlwrap->strlen = p - tlwrap->buf;
nextline = ++p;
} else {
/* can not word wrap - wrap as usual */
*nextline = c;
linewrap = abs(linewrap);
}
}
nlen = strlen(nextline);
nextattr = tlwrap->attr + tlwrap->strlen;
/* if the line was already wrapped then add the rest */
/* at the beginning of the next line */
if(tlwrap->cont && (tlwrap->cont == tlwrap->next)) {
/* if it's a word wrapping don't forget about the extra space */
if(linewrap < 0)
nlen++;
tlwrap->flags |= TLINE_MODIFIED;
/* check if the nextline can be fitted in the buffer */
if(tlwrap->next->strlen + nlen >= tlwrap->next->buflen) {
/* reallocate the buffer space and add a fixed amount of space
to it */
newbuf =
(char *) realloc(tlwrap->next->buf,
tlwrap->next->buflen + nlen + 1);
if(!newbuf) {
tb_error
("tb_wrap_line(): Could not realloc, character not inserted");
return 0;
}
tlwrap->next->buf = newbuf;
newbuf =
(char *) realloc(tlwrap->next->attr,
tlwrap->next->buflen + nlen + 1);
if(!newbuf) {
tb_error
("tb_wrap_line(): Could not realloc attr, character not inserted");
return 0;
}
tlwrap->next->attr = newbuf;
tlwrap->next->buflen = tlwrap->next->buflen + nlen + 1;
}
/* shift the line <nlen> places right */
for(i = tlwrap->next->strlen; i >= 0; i--) {
tlwrap->next->buf[i + nlen] = tlwrap->next->buf[i];
tlwrap->next->attr[i + nlen] = tlwrap->next->attr[i];
}
/* insert the newline */
for(i = 0; i < nlen; i++) {
tlwrap->next->buf[i] = nextline[i];
tlwrap->next->attr[i] = nextattr[i];
}
/* add extra space */
if(linewrap < 0)
tlwrap->next->buf[nlen - 1] = ' ';
/* adjust the string length */
tlwrap->next->strlen += nlen;
} else {
/* otherwise add the rest of the line as a new line */
if(tb->currentline == tb->lastline) {
/* append a line */
tb_append_line(tb, nextline);
} else {
/* insert a line at the next line */
tb_set_next_line(tb);
tb_insert_line(tb, nextline);
tb_set_prev_line(tb);
}
/* mark that the new line is a continuation of the current line */
tlwrap->cont = tlwrap->next;
/* new line has the same colors as current */
tlwrap->next->bgcolor = tlwrap->bgcolor;
tlwrap->next->fgcolor = tlwrap->fgcolor;
}
/* terminate the wrapped line */
tlwrap->buf[tlwrap->strlen] = '\0';
tlwrap->attr[tlwrap->strlen] = '\0';
if(tlwrap->strlen > tb->maxchars)
tb->maxchars = tlwrap->strlen;
while(tlwrap) {
tlwrap->flags |= TLINE_MODIFIED;
tlwrap = tlwrap->next;
}
/* check if new (next) line also needs wrapping */
tb_set_next_line(tb);
tb_wrap_line(tb);
tb_set_prev_line(tb);
return 1;
} /* tb_wrap_line() */
int tb_reformat(TextBuf * tb) {
static int formatting;
int i, pstart, pend;
/* reformat paragraph if needed */
if((i = tb_wrap_line(tb)) == 1) {
if(tb->flags & TEXT_AUTOFORMAT) {
if(formatting)
return i;
formatting = 1;
tb_get_paragraph(tb, &pstart, &pend);
tb_fill_region(tb, tb->i, 0, pend, -1);
formatting = 0;
}
}
return i;
}
void tb_wrap_lines(TextBuf * tb)
/*
* Desc: wrap all lines in buffer
*/ {
if(tb_get_nlines(tb) > 0) {
tb_set_current_line(tb, 0);
tb_wrap_line(tb);
while(tb_set_next_line(tb))
tb_wrap_line(tb);
}
}
void tb_fill_region(TextBuf * tb, int r0, int c0, int r1, int c1)
/*
* Desc: fill specified region with text
*/ {
char *buf, *p;
/* only meaningfull if word wrap is set */
if(tb->linewrap >= 0)
return;
/* get the text */
tb_get_block(tb, r0, c0, r1, c1, &buf);
if(buf == NULL)
return;
/* don't process empty lines */
if(strlen(buf) <= 1) {
free(buf);
return;
}
/* remove all CRs except the last one */
p = buf;
while((p = strchr(p, '\n')) != NULL) {
if(*(p + 1) != '\0')
*p = ' ';
else
break;
}
/* nothing to wrap here */
if(!strchr(buf, ' ')) {
free(buf);
return;
}
/* delete block */
if(tb_del_block(tb, r0, c0, r1, c1) == FALSE) {
free(buf);
return;
}
if((tb_set_current_line(tb, r0) == FALSE) && (r0 > 0)) {
tb_set_current_line(tb, r0 - 1);
tb_append_line(tb, buf);
} else
tb_insert_block(tb, r0, c0, buf);
free(buf);
return;
}
void tb_get_paragraph(TextBuf * tb, int *pstart, int *pend)
/*
* Desc: returns start/end lines number of paragraph around current line
*/ {
TextLine *tl;
int i;
tl = tb->currentline;
i = tb->i;
if(tl->strlen == 0) {
*pstart = *pend = i;
return;
}
while(tl->prev && (tl->prev->strlen > 0) && (i > 0)) {
i--;
tl = tl->prev;
}
*pstart = i;
tl = tb->currentline;
i = tb->i;
while(tl->next && (tl->next->strlen > 0)) {
i++;
tl = tl->next;
}
*pend = i;
return;
}
void tb_modify_lines(TextBuf * tb)
/*
* Desc: mark all lines below current as modified
*/ {
TextLine *tl;
tl = tb->currentline;
while(tl) {
tl->flags |= TLINE_MODIFIED;
tl = tl->next;
}
}
/***********************************************************************
*
* Deletion
*
***********************************************************************/
int tb_del_char(TextBuf * tb, int n)
/*
* Desc: delete the character at the n-th position, returns TRUE
* when the screen needs redrawing
*/ {
int i, l;
char *line, *newbuf;
char *nline, *nattr;
TextLine *tl;
if(n > tb_get_linelen(tb))
return(FALSE);
/* get the current line */
tb_get_line(tb, &line);
if(!line)
return(FALSE);
tb->bufchanged = TRUE;
if(n == tb_get_linelen(tb)) {
/* we are at eol, join the current line with the next one */
if(tb_set_next_line(tb)) {
tb_get_line(tb, &nline);
nattr = tb->currentline->attr;
tb_set_prev_line(tb);
} else
return FALSE;
l = strlen(nline);
tl = tb->currentline;
tl->flags |= TLINE_MODIFIED;
/* check if the next line can be fitted in the buffer */
if(tl->strlen + l >= tl->buflen) {
/* reallocate the buffer space and add a fixed amount of space
to it */
l = tl->strlen + l + TB_BUFINC;
newbuf = (char *) realloc(tl->buf, l);
if(!newbuf) {
tb_error
("tb_del_char(): Could not realloc, character not deleted");
return(FALSE);
}
tl->buf = newbuf;
newbuf = (char *) realloc(tl->attr, l);
if(!newbuf) {
tb_error
("tb_del_char(): Could not realloc attr, character not deleted");
return(FALSE);
}
tl->attr = newbuf;
tl->buflen = l;
}
strcat(tl->buf, nline);
strcat(tl->attr, nattr);
tl->strlen = strlen(tl->buf);
/* delete the next line */
tb_set_next_line(tb);
tb_del_line(tb);
/* restore the current line */
tb->currentline = tl;
/* check if current line needs wrapping */
tb_reformat(tb);
return(TRUE);
}
/* check if the line has any characters in it */
if(tb->currentline->strlen > 0) {
tb->currentline->flags |= TLINE_MODIFIED;
for(i = n; i < tb->currentline->strlen; i++) {
tb->currentline->buf[i] = tb->currentline->buf[i + 1];
tb->currentline->attr[i] = tb->currentline->attr[i + 1];
}
/* adjust strlen */
tb->currentline->strlen--;
}
return(FALSE);
} /* tb_del_char() */
void tb_del_line(TextBuf * tb)
/*
* Desc: delete the current line
*/ {
TextLine *tl = tb->currentline;
if(!tl) {
/* no lines to remove */
return;
}
tb->bufchanged = TRUE;
/* check if we are in the middle or the first line of the text */
if(tl->prev) {
tl->prev->next = tl->next;
/* if there is a next line make that the current one, otherwise
the previous one */
if(tl->next) {
tl->next->prev = tl->prev;
tb->currentline = tl->next;
/* tb->i stays the same */
} else {
/* the current line is the last one in the buffer */
tb->currentline = tl->prev;
if(tb->i > 0)
tb->i--;
/* adjust the last line, we are about to delete it! */
tb->lastline = tl->prev;
}
} else {
/* we are at the first line */
/* see also above */
if(tl->next) {
tl->next->prev = NULL;
tb->currentline = tl->next;
/* adjust the first line, we are about to delete it */
tb->firstline = tl->next;
/* tb->i stays the same */
} else {
/* this must the only line in the buffer */
tb->firstline = NULL;
tb->currentline = NULL;
tb->lastline = NULL;
if(tb->i > 0)
tb->i--;
}
}
free(tl->buf);
free(tl->attr);
free(tl);
tl = tb->currentline;
while(tl) {
tl->flags |= TLINE_MODIFIED;
tl = tl->next;
}
tb->n--;
return;
} /* tb_del_line() */
int tb_del_block(TextBuf * tb, int r0, int c0, int r1, int c1)
/*
* Desc: delete the block at the specified coordinates
*/ {
int i, k, rtop, rbottom, sellen;
if(r0 > r1) {
rtop = r0;
rbottom = 1;
} else {
rtop = r1;
rbottom = r0;
}
tb->bufchanged = TRUE;
if(rtop == rbottom) {
tb_set_current_line(tb, rtop);
if(!tb->currentline)
return FALSE;
if(c1 < 0)
c1 = tb->currentline->strlen;
if(c0 < 0)
c0 = 0;
if(c0 > c1) {
i = c1;
c1 = c0;
c0 = i;
}
if(c1 == c0)
return TRUE;
if((c0 == 0) && (c1 >= tb->currentline->strlen)) {
tb_del_line(tb);
return TRUE;
}
sellen = c1 - c0;
for(i = c1; i <= tb->currentline->strlen; i++) {
tb->currentline->buf[i - sellen] = tb->currentline->buf[i];
tb->currentline->attr[i - sellen] = tb->currentline->attr[i];
}
tb->currentline->strlen -= sellen;
tb->currentline->flags |= TLINE_MODIFIED;
return TRUE;
}
for(i = rbottom; i <= rtop; i++) {
tb_set_current_line(tb, i);
if(!tb->currentline)
continue;
if(i == rtop) {
if((c1 < 0) || (c1 >= tb->currentline->strlen)) {
tb_del_line(tb);
i--;
rtop--;
if(rtop < rbottom)
break;
} else {
for(k = c1; k <= tb->currentline->strlen; k++) {
tb->currentline->buf[k - c1] = tb->currentline->buf[k];
tb->currentline->attr[k - c1] =
tb->currentline->attr[k];
}
tb->currentline->strlen -= c1;
tb->currentline->flags |= TLINE_MODIFIED;
}
} else if(i == rbottom) {
if(c0 == 0) {
tb_del_line(tb);
i--;
rtop--;
if(rtop < rbottom)
break;
} else {
tb->currentline->buf[c0] = '\0';
tb->currentline->attr[c0] = '\0';
tb->currentline->strlen = c0;
tb->currentline->flags |= TLINE_MODIFIED;
}
} else {
tb_del_line(tb);
i--;
rtop--;
if(rtop < rbottom)
break;
}
}
return TRUE;
} /* tb_del_block() */
int tb_handle_tabs(TextBuf * tb)
/*
* Desc: replace TABs with SPACEs in the current line according to <tablen>
* returns number of SPACES that was inserted
*/ {
TextLine *tl = tb->currentline;
char *p, *p1, *newbuf;
int pos, spaces, i, moved;
if(!tl)
return 0;
moved = 0;
/* scan line for TABs */
p = tl->buf;
while((p1 = strchr(p, '\t')) != NULL) {
/* calculate TAB position */
pos = p1 - tl->buf;
/* calculate number of SPACEs to insert */
if(tb->tablen > pos)
spaces = tb->tablen - pos;
else
spaces = tb->tablen - (pos % tb->tablen);
moved += spaces; /* record how much spaces we added */
spaces--;
/* if only one SPACE then replace TAB with it and go on */
if(spaces == 0) {
*p1 = ' ';
p1++;
p = p1;
continue;
}
/* check if the spaces can be fitted in the buffer */
if(tl->strlen + spaces >= tl->buflen) {
/* reallocate the buffer space and add a fixed amount of space
to it */
newbuf = (char *) realloc(tl->buf, tl->buflen + TB_BUFINC);
if(!newbuf) {
tb_error
("tb_insert_char(): Could not realloc, spaces not inserted");
return moved;
}
tl->buf = newbuf;
newbuf = (char *) realloc(tl->attr, tl->buflen + TB_BUFINC);
if(!newbuf) {
tb_error
("tb_insert_char(): Could not realloc attr, spaces not inserted");
return moved;
}
tl->attr = newbuf;
tl->buflen = tl->buflen + TB_BUFINC;
}
/* tl->strlen+spaces < tl->buflen !!! */
/* first move the characters after pos spaces position */
for(i = tl->strlen; i > pos; i--) {
tl->buf[i + spaces] = tl->buf[i];
tl->attr[i + spaces] = tl->attr[i];
}
/* insert spaces */
for(i = pos; i <= pos + spaces; i++) {
tl->buf[i] = ' ';
tl->attr[i] = tb->attr_def;
}
/* adjust strlen */
tl->strlen += spaces;
/* continue the search */
p = tl->buf + pos + 1;
}
if(moved)
tl->flags |= TLINE_MODIFIED;
return moved;
}
/***********************************************************************
*
* Memory buffer
*
***********************************************************************/
void tb_set_text(TextBuf * tb, char *buf, long len)
/*
* Desc: the text from <buf> is loaded into <tb>. The current buffer
* is cleared
* <len> is the length of <buf>. If it's negative <buf> is assumed
* to be NULL terminated.
*/ {
char *t;
long buflen;
/* clear the current buffer */
tb_clear(tb);
tb->bufchanged = FALSE;
if(!buf || (len == 0))
return;
if(len < 0)
len = strlen(buf);
/* scan buffer for newlines */
/* Note: buf can not be modified! */
while((t = (char *) memchr((void *) buf, '\n', len)) != NULL) {
buflen = ++t - buf;
len -= buflen;
buflen--;
if(buflen && (buf[buflen - 1] == '\r'))
buflen--;
tb_append_buf(tb, buf, buflen);
tb_handle_tabs(tb);
tb->currentline = tb->lastline;
tb_set_next_line(tb);
buf = t;
}
if(len > 0)
tb_append_buf(tb, buf, len);
return;
}
u_long tb_get_textlen(TextBuf * tb)
/*
* Desc: calculate length of text in buffer
*/ {
u_long buflen = 0;
TextLine *tl = tb->firstline;
while(tl) {
buflen += tl->strlen;
tl = tl->next;
if(tl)
buflen++;
}
return buflen;
}
char *tb_get_text(TextBuf * tb, u_long * len)
/*
* Desc: put text from <tb> in newly allocated buffer.
* returned buffer should be freed.
* returns pointer to buffer. buffer length is returned in len.
*/ {
char *line, *buf, *p;
u_long *buflen, mlen;
TextLine *tl;
int i;
/* save the current line */
i = tb->i;
tl = tb->currentline;
if(len == NULL)
buflen = &mlen;
else
buflen = len;
/* first caclulate the buffer length */
*buflen = 0;
tb_set_current_line(tb, 0);
do {
tb_get_line(tb, &line);
if(!line)
break;
*buflen += (strlen(line) + 1);
} while(tb_set_next_line(tb));
/* allocate the buffer */
if((buf = (char *) malloc(*buflen + 1)) == NULL) {
tb_error("Can not allocate text buffer");
tb->i = i;
tb->currentline = tl;
return NULL;
}
/* insert text into the buffer */
p = buf;
*p = '\0';
tb_set_current_line(tb, 0);
do {
tb_get_line(tb, &line);
if(!line)
break;
strcpy(p, line);
p += strlen(line);
*p = '\n';
p++;
*p = '\0';
} while(tb_set_next_line(tb));
tb->bufchanged = FALSE;
/* remove last CR
if (*buflen > 0) {
(*buflen)--;
buf[*buflen] = '\0';
}
*/
/* restore the current line */
tb->i = i;
tb->currentline = tl;
return buf;
}
/***********************************************************************
*
* File I/O
*
***********************************************************************/
void tb_load_file(TextBuf * tb, char *fname)
/*
* Desc: the file <fname> is loaded into <tb>. The current buffer
* is cleared.
* Note: This routine can be highly optimized, aswell as tb_append_line()
*/ {
FILE *f;
char line[8192]; /* this limits the loadable line length to 8192 */
f = fopen(fname, "r");
if(!f) {
tb_error("Could not read file %s", fname);
return;
}
/* clear the current buffer */
tb_clear(tb);
while(fgets(line, 8192, f)) {
tb_append_line(tb, line);
tb_handle_tabs(tb);
/* change tabs to spaces */
tb->currentline = tb->lastline;
tb_set_next_line(tb);
}
fclose(f);
tb->bufchanged = FALSE;
return;
} /* tb_load_file() */
void tb_insert_file(TextBuf * tb, int r, int c, char *fname)
/*
* Desc: insert file <fname> starting from (r,c) position
*/ {
FILE *f;
char line[8192]; /* this limits the loadable line length to 8192 */
TextLine *tl, *tlold;
int len, oldwrap;
/* store the current line */
tlold = tb->currentline;
/* no lines yet. insert as a new line(s) */
if((tlold == NULL) && (r == 0)) {
tb_load_file(tb, fname);
return;
}
/* check if line <r> exists */
if(tb_set_current_line(tb, r) == FALSE)
return;
/* check if position is valid */
if(c > tb->currentline->strlen) {
tb->currentline = tlold;
return;
}
tl = tb->currentline;
f = fopen(fname, "r");
if(!f) {
tb_error("Could not read file %s", fname);
return;
}
/*read the first line */
if(!fgets(line, 8191, f)) {
fclose(f);
return;
}
/* make sure that line ends with CR */
len = strlen(line);
if(line[len - 1] != '\n') {
line[len] = '\n';
line[len + 1] = '\0';
}
/* temporary disable wrapping */
oldwrap = tb->linewrap;
tb->linewrap = 0;
/* insert it as a block */
tb_insert_block(tb, r, c, line);
tb_set_next_line(tb);
while(fgets(line, 8191, f)) {
/* insert each line */
tb_insert_line(tb, line);
tb_set_next_line(tb);
}
fclose(f);
/* rewrap everything */
tb->linewrap = oldwrap;
tb_wrap_lines(tb);
tb->currentline = tlold;
return;
}
void tb_save_file(TextBuf * tb, char *fname)
/*
* Desc: save the current buffer <tb> to the file <fname>
*/ {
FILE *f;
char *line;
f = fopen(fname, "w");
if(!f) {
tb_error("Could not save to file %s", fname);
return;
}
tb_set_current_line(tb, 0);
do {
tb_get_line(tb, &line);
if(!line)
break;
fprintf(f, "%s\n", line);
} while(tb_set_next_line(tb));
fclose(f);
tb->bufchanged = FALSE;
return;
} /* tb_save_file() */
/***********************************************************************
*
* Miscellaneous
*
***********************************************************************/
int tb_set_current_line(TextBuf * tb, int n)
/*
* Desc: Set the current line to the n-th line
* n == -1 - lastline n == 0 - firstline
*/ {
int i;
TextLine *tl;
i = 0;
tl = tb->firstline;
if(n == -1) {
while(tl && (tl != tb->lastline)) {
tl = tl->next;
i++;
}
if(tl) {
tb->currentline = tl;
tb->i = i;
return TRUE;
}
return FALSE;
}
while(tl && i < n) {
tl = tl->next;
i++;
}
if(tl && i == n) {
/* we've got the right linenr */
tb->currentline = tl;
tb->i = n;
return TRUE;
} else /* the linenr does not exist */
return FALSE;
} /* tb_set_line() */
int tb_set_next_line(TextBuf * tb)
/*
* Desc: set the current line to the next one in the buffer,
* return FALSE if at EOF, else TRUE
*/ {
if(tb->currentline->next) {
tb->currentline = tb->currentline->next;
tb->i++;
return(TRUE);
} else {
return(FALSE);
}
} /* tb_set_next_line() */
int tb_set_prev_line(TextBuf * tb)
/*
* Desc: set the current line to the previous one in the buffer
* return FALSE if at BOF, else TRUE
*/ {
if(tb->currentline->prev) {
tb->currentline = tb->currentline->prev;
tb->i--;
return(TRUE);
} else {
return(FALSE);
}
} /* tb_set_prev_line() */
int tb_get_nlines(TextBuf * tb)
/*
* Desc: return the # of lines in <tb>
*/ {
return(tb->n);
} /* tb_get_nlines() */
int tb_get_current_line_nr(TextBuf * tb)
/*
* Desc: return the index of the current line
*/ {
return(tb->i);
} /* tb_get_current_line_nr() */
int tb_get_linelen(TextBuf * tb)
/*
* Desc: return the lenght of the current line
*/ {
if(tb->currentline)
return tb->currentline->strlen;
else
return(0);
} /* tb_get_linelen() */
void tb_set_linelen(TextBuf * tb, int len)
/*
* Desc: set lenght of current line (cut the line)
*/ {
if(!tb->currentline)
return;
if(len < tb->currentline->strlen) {
tb->bufchanged = TRUE;
tb->currentline->flags |= TLINE_MODIFIED;
tb->currentline->strlen = len;
tb->currentline->attr[len] = '\0';
tb->currentline->buf[len] = '\0';
}
}
void tb_set_linewrap(TextBuf * tb, int wrap)
/*
* Desc: set line (word) wrap length
*/ {
tb->linewrap = wrap;
}
int tb_get_linewrap(TextBuf * tb)
/*
* Desc: get line (word) wrap length
*/ {
return tb->linewrap;
}
void tb_set_linefgcolor(TextBuf * tb, int c)
/*
* Desc: set the current line color
*/ {
TextLine *tl;
tl = tb->currentline;
tl->fgcolor = c;
while(tl->cont && (tl->next == tl->cont)) {
tl = tl->cont;
tl->fgcolor = c;
}
tl = tb->currentline;
while(tl->prev && (tl->prev->cont == tl)) {
tl = tl->prev;
tl->fgcolor = c;
}
return;
} /* tb_set_linefgcolor() */
void tb_set_linebgcolor(TextBuf * tb, int c)
/*
* Desc: set the current bg line color
*/ {
TextLine *tl;
tl = tb->currentline;
tl->bgcolor = c;
while(tl->cont && (tl->next == tl->cont)) {
tl = tl->cont;
tl->bgcolor = c;
}
tl = tb->currentline;
while(tl->prev && (tl->prev->cont == tl)) {
tl = tl->prev;
tl->bgcolor = c;
}
return;
} /* tb_set_linebgcolor() */
int tb_get_linefgcolor(TextBuf * tb)
/*
* Desc: return the fg color for the current line
*/ {
return(tb->currentline->fgcolor);
} /* tb_get_linefgcolor() */
int tb_get_linebgcolor(TextBuf * tb)
/*
* Desc: return the bg color for the current line
*/ {
return(tb->currentline->bgcolor);
} /* tb_get_linebgcolor() */
int tb_bufchanged(TextBuf * tb)
/*
* Desc: return the status of the textbuffer
*/ {
return(tb->bufchanged);
} /* tb_bufchanged() */
void tb_set_bufchanged(TextBuf * tb, int status)
/*
* Desc: set the bufferchanged status
*/ {
tb->bufchanged = status;
return;
} /* tb_set_bufchanged() */
int tb_find_text(TextBuf * tb, char *text, int sline, int spos, int *pos)
/*
* Desc: search for <text> in <tb> starting from line <sline>:<spos>
* Ret: line number of line that contains the text or -1 if text not found
* <pos> returns the position of the text in the line
*/ {
TextLine *tl;
int line;
char *p, *str;
tl = tb->firstline;
line = 0;
while(tl) {
if(line < sline) {
tl = tl->next;
line++;
continue;
}
if(sline == line)
str = tl->buf + spos;
else
str = tl->buf;
if((p = strstr(str, text)) != NULL) {
if(pos)
*pos = p - tl->buf;
return line;
}
tl = tl->next;
line++;
}
return -1;
}
void tb_fix_line(TextLine * tl)
/*
* Take care of all CR and LF characters in the string
*/ {
char *p;
unsigned char c;
p = tl->buf;
while(*p != '\0') {
c = *(unsigned char *) p;
switch(c) {
case '\010':
case 0xa0:
*p = '_';
break;
case '\n':
if(p[1] == '\0') {
*p = '\0';
tl->strlen = strlen(tl->buf);
tl->attr[tl->strlen] = '\0';
return;
}
*p = '_';
break;
case '\r':
if((p[1] == '\n') && (p[2] == '\0')) {
*p = '\0';
tl->strlen = strlen(tl->buf);
tl->attr[tl->strlen] = '\0';
return;
}
*p = '_';
break;
}
p++;
}
tl->attr[tl->strlen] = '\0';
return;
}
int tb_set_flags(TextBuf * tb, int flags)
/*
* Desc: Returns flags value for given textbuf.
* Ret: old flags value. If supplied flags value is -1 then it's not set
*/ {
int oflags = tb->flags;
if(flags >= 0)
tb->flags = flags;
return oflags;
}
textline_callback
tb_set_callback(TextBuf * tb, textline_callback callback, int cbtype,
int data)
/*
* Desc: set callback on current line
* Ret: old callback value
*/ {
textline_callback ocb = NULL;
TextLine *tl;
if(tb->currentline == NULL)
return NULL;
tb->currentline->callback_data = data;
switch(cbtype) {
case FL_TEXTLINE_CLK_CALLBACK:
ocb = tb->currentline->clk_callback;
tb->currentline->clk_callback = callback;
break;
case FL_TEXTLINE_DBL_CALLBACK:
ocb = tb->currentline->dbl_callback;
tb->currentline->dbl_callback = callback;
break;
case FL_TEXTLINE_CUR_CALLBACK:
ocb = tb->currentline->cur_callback;
tb->currentline->cur_callback = callback;
break;
default:
return NULL;
break;
}
tl = tb->currentline;
while(tl->cont && (tl->next == tl->cont)) {
tl = tl->cont;
switch(cbtype) {
case FL_TEXTLINE_CLK_CALLBACK:
tl->clk_callback = callback;
break;
case FL_TEXTLINE_DBL_CALLBACK:
tl->dbl_callback = callback;
break;
case FL_TEXTLINE_CUR_CALLBACK:
tl->cur_callback = callback;
break;
}
}
tl = tb->currentline;
while(tl->prev && (tl->prev->cont == tl)) {
tl = tl->prev;
switch(cbtype) {
case FL_TEXTLINE_CLK_CALLBACK:
tl->clk_callback = callback;
break;
case FL_TEXTLINE_DBL_CALLBACK:
tl->dbl_callback = callback;
break;
case FL_TEXTLINE_CUR_CALLBACK:
tl->cur_callback = callback;
break;
}
}
return ocb;
}
|