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
|
/*******************************************************************************
* *
* search.c -- Nirvana Editor search and replace functions *
* *
* Copyright (c) 1991 Universities Research Association, Inc. *
* All rights reserved. *
* *
* This material resulted from work developed under a Government Contract and *
* is subject to the following license: The Government retains a paid-up, *
* nonexclusive, irrevocable worldwide license to reproduce, prepare derivative *
* works, perform publicly and display publicly by or for the Government, *
* including the right to distribute to other Government contractors. Neither *
* the United States nor the United States Department of Energy, nor any of *
* their employees, makes any warrenty, express or implied, or assumes any *
* legal liability or responsibility for the accuracy, completeness, or *
* usefulness of any information, apparatus, product, or process disclosed, or *
* represents that its use would not infringe privately owned rights. *
* *
* Fermilab Nirvana GUI Library *
* May 10, 1991 *
* *
* Written by Mark Edel *
* *
*******************************************************************************/
#include <stdio.h>
#include <ctype.h>
#ifdef VMS
#include "../util/VMSparam.h"
#else
#include <sys/param.h>
#endif /*VMS*/
#include <Xm/Xm.h>
#include <X11/Shell.h>
#include <Xm/XmP.h>
#include <Xm/Form.h>
#include <Xm/Label.h>
#include <Xm/PushB.h>
#include <Xm/RowColumn.h>
#include <Xm/Text.h>
#include <Xm/ToggleB.h>
#include <X11/Xatom.h> /* for getting selection */
#include <X11/keysym.h>
#ifdef MOTIF10
#include <X11/Selection.h> /* " " */
#endif
#include <X11/X.h> /* " " */
#include "../util/DialogF.h"
#include "../util/misc.h"
#include "regularExp.h"
#include "textBuf.h"
#include "text.h"
#include "nedit.h"
#include "search.h"
#include "window.h"
#include "preferences.h"
/* History mechanism for search and replace strings */
static char *SearchHistory[MAX_SEARCH_HISTORY];
static char *ReplaceHistory[MAX_SEARCH_HISTORY];
static int SearchTypeHistory[MAX_SEARCH_HISTORY];
static int HistStart = 0;
static int NHist = 0;
static void createReplaceDlog(Widget parent, WindowInfo *window);
static void createFindDlog(Widget parent, WindowInfo *window);
static void fFocusCB(Widget w, WindowInfo *window, caddr_t *callData);
static void rFocusCB(Widget w, WindowInfo *window, caddr_t *callData);
static void rKeepCB(Widget w, WindowInfo *window, caddr_t *callData);
static void fKeepCB(Widget w, WindowInfo *window, caddr_t *callData);
static void replaceCB(Widget w, WindowInfo *window,
XmAnyCallbackStruct *callData);
static void replaceAllCB(Widget w, WindowInfo *window,
XmAnyCallbackStruct *callData);
static void rInSelCB(Widget w, WindowInfo *window,
XmAnyCallbackStruct *callData);
static void rCancelCB(Widget w, WindowInfo *window, caddr_t callData);
static void fCancelCB(Widget w, WindowInfo *window, caddr_t callData);
static void rFindCB(Widget w,WindowInfo *window,XmAnyCallbackStruct *callData);
static void rFindArrowKeyCB(Widget w, WindowInfo *window, XKeyEvent *event);
static void replaceArrowKeyCB(Widget w, WindowInfo *window, XKeyEvent *event);
static void findArrowKeyCB(Widget w, WindowInfo *window, XKeyEvent *event);
static void findCB(Widget w, WindowInfo *window,XmAnyCallbackStruct *callData);
static void flashTimeoutProc(XtPointer clientData, XtIntervalId *id);
static void eraseFlash(WindowInfo *window);
static int getReplaceDlogInfo(WindowInfo *window, int *direction,
char *searchString, char *replaceString, int *searchType);
static int getFindDlogInfo(WindowInfo *window, int *direction,
char *searchString, int *searchType);
static void selectedSearchCB(Widget w, XtPointer callData, Atom *selection,
Atom *type, char *value, int *length, int *format);
static int searchLiteral(char *string, char *searchString, int caseSense,
int direction, int wrap, int beginPos, int *startPos, int *endPos);
static int searchRegex(char *string, char *searchString, int direction,
int wrap, int beginPos, int *startPos, int *endPos, char *delimiters);
static int forwardRegexSearch(char *string, char *searchString,
int wrap, int beginPos, int *startPos, int *endPos, char *delimiters);
static int backwardRegexSearch(char *string, char *searchString,
int wrap, int beginPos, int *startPos, int *endPos, char *delimiters);
static void upCaseString(char *outString, char *inString);
static void downCaseString(char *outString, char *inString);
static void resetFindTabGroup(WindowInfo *window);
static void resetReplaceTabGroup(WindowInfo *window);
static int searchMatchesSelection(WindowInfo *window, char *searchString,
int searchType, int *left, int *right);
static int findMatchingChar(textBuffer *buf, char toMatch, int charPos,
int startLimit, int endLimit, int *matchPos);
static void replaceUsingRE(char *searchStr, char *replaceStr, char *sourceStr,
char *destStr, int maxDestLen, char *delimiters);
static void saveSearchHistory(char *searchString, char *replaceString,
int searchType);
static int historyIndex(int nCycles);
static char *searchTypeArg(int searchType);
static char *directionArg(int direction);
static int isStartOfLine(char *string, int beginPos);
static int isStartOfWord(char *string, int beginPos);
typedef struct _charMatchTable {
char c;
char match;
char direction;
} charMatchTable;
#define N_MATCH_CHARS 13
#define N_FLASH_CHARS 6
static charMatchTable MatchingChars[N_MATCH_CHARS] = {
{'{', '}', SEARCH_FORWARD},
{'}', '{', SEARCH_BACKWARD},
{'(', ')', SEARCH_FORWARD},
{')', '(', SEARCH_BACKWARD},
{'[', ']', SEARCH_FORWARD},
{']', '[', SEARCH_BACKWARD},
{'<', '>', SEARCH_FORWARD},
{'>', '<', SEARCH_BACKWARD},
{'/', '/', SEARCH_FORWARD},
{'"', '"', SEARCH_FORWARD},
{'\'', '\'', SEARCH_FORWARD},
{'`', '`', SEARCH_FORWARD},
{'\\', '\\', SEARCH_FORWARD},
};
void DoReplaceDlog(WindowInfo *window, int direction)
{
Widget button;
/* Create the dialog if it doesn't already exist */
if (window->replaceDlog == NULL)
createReplaceDlog(window->shell, window);
/* If the window is already up, just pop it to the top */
if (XtIsManaged(window->replaceDlog)) {
RaiseShellWindow(XtParent(window->replaceDlog));
return;
}
/* Set the initial search type */
switch (GetPrefSearch()) {
case SEARCH_LITERAL:
button = window->replaceLiteralBtn;
break;
case SEARCH_CASE_SENSE:
button = window->replaceCaseBtn;
break;
case SEARCH_REGEX:
button = window->replaceRegExpBtn;
break;
}
XmToggleButtonSetState(button, True, True);
/* Set the initial direction based on the direction argument */
XmToggleButtonSetState(direction == SEARCH_FORWARD ?
window->replaceFwdBtn : window->replaceRevBtn, True, True);
/* Set the state of the Keep Dialog Up button */
XmToggleButtonSetState(window->replaceKeepBtn, GetPrefKeepSearchDlogs(),
True);
/* Blank the text fields */
XmTextSetString(window->replaceText, "");
XmTextSetString(window->replaceWithText, "");
/* Start the search history mechanism at the current history item */
window->rHistIndex = 0;
/* Display the dialog */
ManageDialogCenteredOnPointer(window->replaceDlog);
}
void DoFindDlog(WindowInfo *window, int direction)
{
Widget button;
/* Create the dialog if it doesn't already exist */
if (window->findDlog == NULL)
createFindDlog(window->shell, window);
/* If the window is already up, just pop it to the top */
if (XtIsManaged(window->findDlog)) {
RaiseShellWindow(XtParent(window->findDlog));
return;
}
/* Set the initial search type */
switch (GetPrefSearch()) {
case SEARCH_LITERAL:
button = window->findLiteralBtn;
break;
case SEARCH_CASE_SENSE:
button = window->findCaseBtn;
break;
case SEARCH_REGEX:
button = window->findRegExpBtn;
break;
}
XmToggleButtonSetState(button, True, True);
/* Set the initial direction based on the direction argument */
XmToggleButtonSetState(direction == SEARCH_FORWARD ?
window->findFwdBtn : window->findRevBtn, True, True);
/* Set the state of the Keep Dialog Up button */
XmToggleButtonSetState(window->findKeepBtn, GetPrefKeepSearchDlogs(),
True);
/* Blank the text field */
XmTextSetString(window->findText, "");
/* start the search history mechanism at the current history item */
window->fHistIndex = 0;
/* Display the dialog */
ManageDialogCenteredOnPointer(window->findDlog);
}
static void createReplaceDlog(Widget parent, WindowInfo *window)
{
Arg args[50];
int argcnt, defaultBtnOffset;
XmString st1;
Widget form, btnForm;
Widget searchTypeBox, literalBtn, caseBtn, regExpBtn;
Widget label2, label1, label, replaceText, findText;
Widget findBtn, replaceAllBtn, rInSelBtn, cancelBtn, replaceBtn;
Widget searchDirBox, forwardBtn, reverseBtn, keepBtn;
char title[MAXPATHLEN + 14];
Dimension shadowThickness;
argcnt = 0;
XtSetArg(args[argcnt], XmNautoUnmanage, False); argcnt++;
form = XmCreateFormDialog(parent, "replaceDialog", args, argcnt);
XtVaSetValues(form, XmNshadowThickness, 0, 0);
if (GetPrefKeepSearchDlogs()) {
sprintf(title, "Replace (in %s)", window->filename);
XtVaSetValues(XtParent(form), XmNtitle, title, 0);
} else
XtVaSetValues(XtParent(form), XmNtitle, "Replace", 0);
argcnt = 0;
XtSetArg(args[argcnt], XmNtopAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNbottomAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNleftAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNrightAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNleftOffset, 4); argcnt++;
XtSetArg(args[argcnt], XmNtopOffset, 6); argcnt++;
XtSetArg(args[argcnt], XmNalignment, XmALIGNMENT_BEGINNING); argcnt++;
XtSetArg(args[argcnt], XmNlabelString, st1=MKSTRING("String to Find:"));
argcnt++;
XtSetArg(args[argcnt], XmNmnemonic, 't'); argcnt++;
label1 = XmCreateLabel(form, "label1", args, argcnt);
XmStringFree(st1);
XtManageChild(label1);
argcnt = 0;
XtSetArg(args[argcnt], XmNtopAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNbottomAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNleftAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNrightAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNrightOffset, 6); argcnt++;
XtSetArg(args[argcnt], XmNtopOffset, 6); argcnt++;
XtSetArg(args[argcnt], XmNalignment, XmALIGNMENT_END); argcnt++;
XtSetArg(args[argcnt], XmNlabelString, st1=MKSTRING(
"(use up arrow key to recall previous)")); argcnt++;
label2 = XmCreateLabel(form, "label2", args, argcnt);
XmStringFree(st1);
XtManageChild(label2);
argcnt = 0;
XtSetArg(args[argcnt], XmNtraversalOn, True); argcnt++;
XtSetArg(args[argcnt], XmNhighlightThickness, 2); argcnt++;
XtSetArg(args[argcnt], XmNtopAttachment, XmATTACH_WIDGET); argcnt++;
XtSetArg(args[argcnt], XmNbottomAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNleftAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNrightAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNtopWidget, label1); argcnt++;
XtSetArg(args[argcnt], XmNleftOffset, 6); argcnt++;
XtSetArg(args[argcnt], XmNrightOffset, 6); argcnt++;
XtSetArg(args[argcnt], XmNmaxLength, SEARCHMAX); argcnt++;
findText = XmCreateText(form, "replaceString", args, argcnt);
XtAddCallback(findText, XmNfocusCallback, (XtCallbackProc)rFocusCB, window);
XtAddEventHandler(findText, KeyPressMask, False,
(XtEventHandler)rFindArrowKeyCB, window);
RemapDeleteKey(findText);
XtManageChild(findText);
XmAddTabGroup(findText);
XtVaSetValues(label1, XmNuserData, findText, 0); /* mnemonic processing */
argcnt = 0;
XtSetArg(args[argcnt], XmNtopAttachment, XmATTACH_WIDGET); argcnt++;
XtSetArg(args[argcnt], XmNbottomAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNleftAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNrightAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNtopWidget, findText); argcnt++;
XtSetArg(args[argcnt], XmNleftOffset, 6); argcnt++;
XtSetArg(args[argcnt], XmNtopOffset, 6); argcnt++;
XtSetArg(args[argcnt], XmNalignment, XmALIGNMENT_BEGINNING); argcnt++;
XtSetArg(args[argcnt], XmNlabelString,
st1=MKSTRING("Replace With:")); argcnt++;
XtSetArg(args[argcnt], XmNmnemonic, 'W'); argcnt++;
label = XmCreateLabel(form, "label", args, argcnt);
XmStringFree(st1);
XtManageChild(label);
argcnt = 0;
XtSetArg(args[argcnt], XmNtraversalOn, True); argcnt++;
XtSetArg(args[argcnt], XmNhighlightThickness, 2); argcnt++;
XtSetArg(args[argcnt], XmNtopAttachment, XmATTACH_WIDGET); argcnt++;
XtSetArg(args[argcnt], XmNbottomAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNleftAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNrightAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNtopWidget, label); argcnt++;
XtSetArg(args[argcnt], XmNleftOffset, 6); argcnt++;
XtSetArg(args[argcnt], XmNrightOffset, 6); argcnt++;
XtSetArg(args[argcnt], XmNmaxLength, SEARCHMAX); argcnt++;
replaceText = XmCreateText(form, "replaceWithString", args, argcnt);
XtAddEventHandler(replaceText, KeyPressMask, False,
(XtEventHandler)replaceArrowKeyCB, window);
RemapDeleteKey(replaceText);
XtManageChild(replaceText);
XmAddTabGroup(replaceText);
XtVaSetValues(label, XmNuserData, replaceText, 0); /* mnemonic processing */
argcnt = 0;
XtSetArg(args[argcnt], XmNorientation, XmHORIZONTAL); argcnt++;
XtSetArg(args[argcnt], XmNpacking, XmPACK_TIGHT); argcnt++;
XtSetArg(args[argcnt], XmNmarginHeight, 0); argcnt++;
XtSetArg(args[argcnt], XmNtopAttachment, XmATTACH_WIDGET); argcnt++;
XtSetArg(args[argcnt], XmNleftAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNrightAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNtopWidget, replaceText); argcnt++;
XtSetArg(args[argcnt], XmNleftOffset, 2); argcnt++;
XtSetArg(args[argcnt], XmNrightOffset, 4); argcnt++;
XtSetArg(args[argcnt], XmNradioBehavior, True); argcnt++;
XtSetArg(args[argcnt], XmNradioAlwaysOne, True); argcnt++;
searchTypeBox = XmCreateRowColumn(form, "searchTypeBox", args, argcnt);
XtManageChild(searchTypeBox);
XmAddTabGroup(searchTypeBox);
argcnt = 0;
XtSetArg(args[argcnt], XmNtraversalOn, True); argcnt++;
XtSetArg(args[argcnt], XmNhighlightThickness, 2); argcnt++;
XtSetArg(args[argcnt], XmNlabelString,
st1=MKSTRING("Literal")); argcnt++;
XtSetArg(args[argcnt], XmNmnemonic, 'L'); argcnt++;
literalBtn = XmCreateToggleButton(searchTypeBox, "literal", args, argcnt);
XmStringFree(st1);
XtManageChild(literalBtn);
argcnt = 0;
XtSetArg(args[argcnt], XmNtraversalOn, True); argcnt++;
XtSetArg(args[argcnt], XmNhighlightThickness, 2); argcnt++;
XtSetArg(args[argcnt], XmNlabelString,
st1=MKSTRING("Case Sensitive Literal")); argcnt++;
XtSetArg(args[argcnt], XmNmnemonic, 'C'); argcnt++;
caseBtn = XmCreateToggleButton(searchTypeBox, "caseSenseLiteral", args, argcnt);
XmStringFree(st1);
XtManageChild(caseBtn);
argcnt = 0;
XtSetArg(args[argcnt], XmNtraversalOn, True); argcnt++;
XtSetArg(args[argcnt], XmNhighlightThickness, 2); argcnt++;
XtSetArg(args[argcnt], XmNlabelString,
st1=MKSTRING("Regular Expression")); argcnt++;
XtSetArg(args[argcnt], XmNmnemonic, 'R'); argcnt++;
regExpBtn = XmCreateToggleButton(searchTypeBox, "regExp", args, argcnt);
XmStringFree(st1);
XtManageChild(regExpBtn);
argcnt = 0;
XtSetArg(args[argcnt], XmNorientation, XmHORIZONTAL); argcnt++;
XtSetArg(args[argcnt], XmNpacking, XmPACK_TIGHT); argcnt++;
XtSetArg(args[argcnt], XmNmarginHeight, 0); argcnt++;
XtSetArg(args[argcnt], XmNtopAttachment, XmATTACH_WIDGET); argcnt++;
XtSetArg(args[argcnt], XmNtopOffset, 0); argcnt++;
XtSetArg(args[argcnt], XmNtopWidget, searchTypeBox); argcnt++;
XtSetArg(args[argcnt], XmNleftAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNleftOffset, 2); argcnt++;
XtSetArg(args[argcnt], XmNradioBehavior, True); argcnt++;
XtSetArg(args[argcnt], XmNradioAlwaysOne, True); argcnt++;
searchDirBox = XmCreateRowColumn(form, "searchDirBox", args, argcnt);
XtManageChild(searchDirBox);
XmAddTabGroup(searchDirBox);
argcnt = 0;
XtSetArg(args[argcnt], XmNlabelString,
st1=MKSTRING("Search Forward")); argcnt++;
XtSetArg(args[argcnt], XmNmnemonic, 'o'); argcnt++;
forwardBtn = XmCreateToggleButton(searchDirBox, "forward", args, argcnt);
XmStringFree(st1);
XtManageChild(forwardBtn);
argcnt = 0;
XtSetArg(args[argcnt], XmNlabelString,
st1=MKSTRING("Search Backward")); argcnt++;
XtSetArg(args[argcnt], XmNmnemonic, 'B'); argcnt++;
reverseBtn = XmCreateToggleButton(searchDirBox, "reverse", args, argcnt);
XmStringFree(st1);
XtManageChild(reverseBtn);
argcnt = 0;
XtSetArg(args[argcnt], XmNlabelString,
st1=MKSTRING("Keep Dialog")); argcnt++;
XtSetArg(args[argcnt], XmNmnemonic, 'K'); argcnt++;
XtSetArg(args[argcnt], XmNtopAttachment, XmATTACH_WIDGET); argcnt++;
XtSetArg(args[argcnt], XmNtopOffset, 0); argcnt++;
XtSetArg(args[argcnt], XmNtopWidget, searchTypeBox); argcnt++;
XtSetArg(args[argcnt], XmNrightAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNrightOffset, 4); argcnt++;
keepBtn = XmCreateToggleButton(form, "keep", args, argcnt);
XtAddCallback(keepBtn, XmNvalueChangedCallback,
(XtCallbackProc)rKeepCB, window);
XmStringFree(st1);
XtManageChild(keepBtn);
XmAddTabGroup(keepBtn);
argcnt = 0;
XtSetArg(args[argcnt], XmNleftAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNrightAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNtopAttachment, XmATTACH_WIDGET); argcnt++;
XtSetArg(args[argcnt], XmNbottomAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNtopWidget, searchDirBox); argcnt++;
XtSetArg(args[argcnt], XmNleftOffset, 6); argcnt++;
XtSetArg(args[argcnt], XmNrightOffset, 6); argcnt++;
btnForm = XmCreateForm(form, "buttons", args, argcnt);
XtManageChild(btnForm);
XmAddTabGroup(btnForm);
argcnt = 0;
XtSetArg(args[argcnt], XmNtraversalOn, True); argcnt++;
XtSetArg(args[argcnt], XmNhighlightThickness, 2); argcnt++;
XtSetArg(args[argcnt], XmNlabelString, st1=MKSTRING("Replace")); argcnt++;
XtSetArg(args[argcnt], XmNshowAsDefault, (short)1); argcnt++;
XtSetArg(args[argcnt], XmNtopAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNleftAttachment, XmATTACH_POSITION); argcnt++;
XtSetArg(args[argcnt], XmNrightAttachment, XmATTACH_POSITION); argcnt++;
XtSetArg(args[argcnt], XmNbottomAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNleftPosition, 0); argcnt++;
XtSetArg(args[argcnt], XmNrightPosition, 21); argcnt++;
replaceBtn = XmCreatePushButton(btnForm, "replace", args, argcnt);
XtAddCallback(replaceBtn, XmNactivateCallback, (XtCallbackProc)replaceCB,
window);
XmStringFree(st1);
XtManageChild(replaceBtn);
XtVaGetValues(replaceBtn, XmNshadowThickness, &shadowThickness, 0);
defaultBtnOffset = shadowThickness + 4;
argcnt = 0;
XtSetArg(args[argcnt], XmNtraversalOn, True); argcnt++;
XtSetArg(args[argcnt], XmNhighlightThickness, 2); argcnt++;
XtSetArg(args[argcnt], XmNlabelString, st1=MKSTRING("Find")); argcnt++;
XtSetArg(args[argcnt], XmNmnemonic, 'F'); argcnt++;
XtSetArg(args[argcnt], XmNtopAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNbottomAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNleftAttachment, XmATTACH_POSITION); argcnt++;
XtSetArg(args[argcnt], XmNrightAttachment, XmATTACH_POSITION); argcnt++;
XtSetArg(args[argcnt], XmNleftPosition, 21); argcnt++;
XtSetArg(args[argcnt], XmNrightPosition, 33); argcnt++;
XtSetArg(args[argcnt], XmNtopOffset, defaultBtnOffset); argcnt++;
findBtn = XmCreatePushButton(btnForm, "find", args, argcnt);
XtAddCallback(findBtn, XmNactivateCallback, (XtCallbackProc)rFindCB,window);
XmStringFree(st1);
XtManageChild(findBtn);
argcnt = 0;
XtSetArg(args[argcnt], XmNtraversalOn, True); argcnt++;
XtSetArg(args[argcnt], XmNhighlightThickness, 2); argcnt++;
XtSetArg(args[argcnt], XmNlabelString,
st1=MKSTRING("Replace All")); argcnt++;
XtSetArg(args[argcnt], XmNmnemonic, 'A'); argcnt++;
XtSetArg(args[argcnt], XmNtopAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNbottomAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNleftAttachment, XmATTACH_POSITION); argcnt++;
XtSetArg(args[argcnt], XmNrightAttachment, XmATTACH_POSITION); argcnt++;
XtSetArg(args[argcnt], XmNleftPosition, 33); argcnt++;
XtSetArg(args[argcnt], XmNrightPosition, 56); argcnt++;
XtSetArg(args[argcnt], XmNtopOffset, defaultBtnOffset); argcnt++;
replaceAllBtn = XmCreatePushButton(btnForm, "all", args, argcnt);
XtAddCallback(replaceAllBtn, XmNactivateCallback,
(XtCallbackProc)replaceAllCB, window);
XmStringFree(st1);
XtManageChild(replaceAllBtn);
argcnt = 0;
XtSetArg(args[argcnt], XmNtraversalOn, True); argcnt++;
XtSetArg(args[argcnt], XmNhighlightThickness, 2); argcnt++;
XtSetArg(args[argcnt], XmNlabelString,
st1=MKSTRING("R. In Selection")); argcnt++;
XtSetArg(args[argcnt], XmNmnemonic, 'S'); argcnt++;
XtSetArg(args[argcnt], XmNsensitive, window->wasSelected); argcnt++;
XtSetArg(args[argcnt], XmNtopAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNbottomAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNleftAttachment, XmATTACH_POSITION); argcnt++;
XtSetArg(args[argcnt], XmNrightAttachment, XmATTACH_POSITION); argcnt++;
XtSetArg(args[argcnt], XmNleftPosition, 56); argcnt++;
XtSetArg(args[argcnt], XmNrightPosition, 85); argcnt++;
XtSetArg(args[argcnt], XmNtopOffset, defaultBtnOffset); argcnt++;
rInSelBtn = XmCreatePushButton(btnForm, "inSel", args, argcnt);
XtAddCallback(rInSelBtn, XmNactivateCallback, (XtCallbackProc)rInSelCB,
window);
XmStringFree(st1);
XtManageChild(rInSelBtn);
argcnt = 0;
XtSetArg(args[argcnt], XmNtraversalOn, True); argcnt++;
XtSetArg(args[argcnt], XmNhighlightThickness, 2); argcnt++;
XtSetArg(args[argcnt], XmNlabelString, st1=MKSTRING("Cancel")); argcnt++;
XtSetArg(args[argcnt], XmNtopAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNbottomAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNleftAttachment, XmATTACH_POSITION); argcnt++;
XtSetArg(args[argcnt], XmNrightAttachment, XmATTACH_POSITION); argcnt++;
XtSetArg(args[argcnt], XmNleftPosition, 85); argcnt++;
XtSetArg(args[argcnt], XmNrightPosition, 100); argcnt++;
XtSetArg(args[argcnt], XmNtopOffset, defaultBtnOffset); argcnt++;
cancelBtn = XmCreatePushButton(btnForm, "cancel", args, argcnt);
XmStringFree(st1);
XtAddCallback(cancelBtn, XmNactivateCallback, (XtCallbackProc)rCancelCB,
window);
XtManageChild(cancelBtn);
XtVaSetValues(form, XmNcancelButton, cancelBtn, 0);
AddDialogMnemonicHandler(form);
window->replaceDlog = form;
window->replaceText = findText;
window->replaceWithText = replaceText;
window->replaceLiteralBtn = literalBtn;
window->replaceCaseBtn = caseBtn;
window->replaceRegExpBtn = regExpBtn;
window->replaceFwdBtn = forwardBtn;
window->replaceRevBtn = reverseBtn;
window->replaceKeepBtn = keepBtn;
window->replaceBtns = btnForm;
window->replaceBtn = replaceBtn;
window->replaceInSelBtn = rInSelBtn;
window->replaceSearchTypeBox = searchTypeBox;
}
static void createFindDlog(Widget parent, WindowInfo *window)
{
Arg args[50];
int argcnt, defaultBtnOffset;
XmString st1;
Widget form, btnForm, searchTypeBox, literalBtn, caseBtn, regExpBtn;
Widget findText, label1, label2, cancelBtn, findBtn;
Widget searchDirBox, forwardBtn, reverseBtn, keepBtn;
char title[MAXPATHLEN + 11];
Dimension shadowThickness;
argcnt = 0;
XtSetArg(args[argcnt], XmNautoUnmanage, False); argcnt++;
form = XmCreateFormDialog(parent, "findDialog", args, argcnt);
XtVaSetValues(form, XmNshadowThickness, 0, 0);
if (GetPrefKeepSearchDlogs()) {
sprintf(title, "Find (in %s)", window->filename);
XtVaSetValues(XtParent(form), XmNtitle, title, 0);
} else
XtVaSetValues(XtParent(form), XmNtitle, "Find", 0);
argcnt = 0;
XtSetArg(args[argcnt], XmNtopAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNbottomAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNleftAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNrightAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNleftOffset, 6); argcnt++;
XtSetArg(args[argcnt], XmNtopOffset, 6); argcnt++;
XtSetArg(args[argcnt], XmNalignment, XmALIGNMENT_BEGINNING); argcnt++;
XtSetArg(args[argcnt], XmNlabelString, st1=MKSTRING("String to Find:"));
argcnt++;
XtSetArg(args[argcnt], XmNmnemonic, 'S'); argcnt++;
label1 = XmCreateLabel(form, "label1", args, argcnt);
XmStringFree(st1);
XtManageChild(label1);
argcnt = 0;
XtSetArg(args[argcnt], XmNtopAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNbottomAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNleftAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNrightAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNrightOffset, 6); argcnt++;
XtSetArg(args[argcnt], XmNtopOffset, 6); argcnt++;
XtSetArg(args[argcnt], XmNalignment, XmALIGNMENT_END); argcnt++;
XtSetArg(args[argcnt], XmNlabelString, st1=MKSTRING(
"(use up arrow key to recall previous)")); argcnt++;
label2 = XmCreateLabel(form, "label2", args, argcnt);
XmStringFree(st1);
XtManageChild(label2);
argcnt = 0;
XtSetArg(args[argcnt], XmNtraversalOn, True); argcnt++;
XtSetArg(args[argcnt], XmNhighlightThickness, 2); argcnt++;
XtSetArg(args[argcnt], XmNtopAttachment, XmATTACH_WIDGET); argcnt++;
XtSetArg(args[argcnt], XmNbottomAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNleftAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNrightAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNtopWidget, label1); argcnt++;
XtSetArg(args[argcnt], XmNleftOffset, 6); argcnt++;
XtSetArg(args[argcnt], XmNrightOffset, 6); argcnt++;
XtSetArg(args[argcnt], XmNmaxLength, SEARCHMAX); argcnt++;
findText = XmCreateText(form, "searchString", args, argcnt);
XtAddCallback(findText, XmNfocusCallback, (XtCallbackProc)fFocusCB, window);
XtAddEventHandler(findText, KeyPressMask, False,
(XtEventHandler)findArrowKeyCB, window);
RemapDeleteKey(findText);
XtManageChild(findText);
XmAddTabGroup(findText);
XtVaSetValues(label1, XmNuserData, findText, 0); /* mnemonic processing */
argcnt = 0;
XtSetArg(args[argcnt], XmNorientation, XmHORIZONTAL); argcnt++;
XtSetArg(args[argcnt], XmNpacking, XmPACK_TIGHT); argcnt++;
XtSetArg(args[argcnt], XmNmarginHeight, 0); argcnt++;
XtSetArg(args[argcnt], XmNtopAttachment, XmATTACH_WIDGET); argcnt++;
XtSetArg(args[argcnt], XmNleftAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNrightAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNtopWidget, findText); argcnt++;
XtSetArg(args[argcnt], XmNleftOffset, 2); argcnt++;
XtSetArg(args[argcnt], XmNrightOffset, 4); argcnt++;
XtSetArg(args[argcnt], XmNradioBehavior, True); argcnt++;
XtSetArg(args[argcnt], XmNradioAlwaysOne, True); argcnt++;
searchTypeBox = XmCreateRowColumn(form, "searchTypeBox", args, argcnt);
XtManageChild(searchTypeBox);
XmAddTabGroup(searchTypeBox);
argcnt = 0;
XtSetArg(args[argcnt], XmNtraversalOn, True); argcnt++;
XtSetArg(args[argcnt], XmNhighlightThickness, 2); argcnt++;
XtSetArg(args[argcnt], XmNlabelString, st1=MKSTRING("Literal")); argcnt++;
XtSetArg(args[argcnt], XmNmnemonic, 'L'); argcnt++;
literalBtn = XmCreateToggleButton(searchTypeBox, "literal", args, argcnt);
XmStringFree(st1);
XtManageChild(literalBtn);
argcnt = 0;
XtSetArg(args[argcnt], XmNtraversalOn, True); argcnt++;
XtSetArg(args[argcnt], XmNhighlightThickness, 2); argcnt++;
XtSetArg(args[argcnt], XmNlabelString,
st1=MKSTRING("Case Sensitive Literal")); argcnt++;
XtSetArg(args[argcnt], XmNmnemonic, 'C'); argcnt++;
caseBtn = XmCreateToggleButton(searchTypeBox, "caseSenseLiteral", args, argcnt);
XmStringFree(st1);
XtManageChild(caseBtn);
argcnt = 0;
XtSetArg(args[argcnt], XmNtraversalOn, True); argcnt++;
XtSetArg(args[argcnt], XmNhighlightThickness, 2); argcnt++;
XtSetArg(args[argcnt], XmNlabelString,
st1=MKSTRING("Regular Expression")); argcnt++;
XtSetArg(args[argcnt], XmNmnemonic, 'R'); argcnt++;
regExpBtn = XmCreateToggleButton(searchTypeBox, "regExp", args, argcnt);
XmStringFree(st1);
XtManageChild(regExpBtn);
argcnt = 0;
XtSetArg(args[argcnt], XmNorientation, XmHORIZONTAL); argcnt++;
XtSetArg(args[argcnt], XmNpacking, XmPACK_TIGHT); argcnt++;
XtSetArg(args[argcnt], XmNmarginHeight, 0); argcnt++;
XtSetArg(args[argcnt], XmNtopAttachment, XmATTACH_WIDGET); argcnt++;
XtSetArg(args[argcnt], XmNtopOffset, 0); argcnt++;
XtSetArg(args[argcnt], XmNtopWidget, searchTypeBox); argcnt++;
XtSetArg(args[argcnt], XmNleftAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNleftOffset, 2); argcnt++;
XtSetArg(args[argcnt], XmNradioBehavior, True); argcnt++;
XtSetArg(args[argcnt], XmNradioAlwaysOne, True); argcnt++;
searchDirBox = XmCreateRowColumn(form, "searchDirBox", args, argcnt);
XtManageChild(searchDirBox);
XmAddTabGroup(searchDirBox);
argcnt = 0;
XtSetArg(args[argcnt], XmNlabelString,
st1=MKSTRING("Search Forward")); argcnt++;
XtSetArg(args[argcnt], XmNmnemonic, 'o'); argcnt++;
forwardBtn = XmCreateToggleButton(searchDirBox, "forward", args, argcnt);
XmStringFree(st1);
XtManageChild(forwardBtn);
argcnt = 0;
XtSetArg(args[argcnt], XmNlabelString,
st1=MKSTRING("Search Backward")); argcnt++;
XtSetArg(args[argcnt], XmNmnemonic, 'B'); argcnt++;
reverseBtn = XmCreateToggleButton(searchDirBox, "reverse", args, argcnt);
XmStringFree(st1);
XtManageChild(reverseBtn);
argcnt = 0;
XtSetArg(args[argcnt], XmNlabelString,
st1=MKSTRING("Keep Dialog")); argcnt++;
XtSetArg(args[argcnt], XmNmnemonic, 'K'); argcnt++;
XtSetArg(args[argcnt], XmNtopAttachment, XmATTACH_WIDGET); argcnt++;
XtSetArg(args[argcnt], XmNtopOffset, 0); argcnt++;
XtSetArg(args[argcnt], XmNtopWidget, searchTypeBox); argcnt++;
XtSetArg(args[argcnt], XmNrightAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNrightOffset, 4); argcnt++;
keepBtn = XmCreateToggleButton(form, "keep", args, argcnt);
XtAddCallback(keepBtn, XmNvalueChangedCallback,
(XtCallbackProc)fKeepCB, window);
XmStringFree(st1);
XtManageChild(keepBtn);
XmAddTabGroup(keepBtn);
argcnt = 0;
XtSetArg(args[argcnt], XmNleftAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNrightAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNtopAttachment, XmATTACH_WIDGET); argcnt++;
XtSetArg(args[argcnt], XmNbottomAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNtopWidget, searchDirBox); argcnt++;
XtSetArg(args[argcnt], XmNleftOffset, 2); argcnt++;
XtSetArg(args[argcnt], XmNrightOffset, 4); argcnt++;
btnForm = XmCreateForm(form, "buttons", args, argcnt);
XtManageChild(btnForm);
XmAddTabGroup(btnForm);
argcnt = 0;
XtSetArg(args[argcnt], XmNtraversalOn, True); argcnt++;
XtSetArg(args[argcnt], XmNhighlightThickness, 2); argcnt++;
XtSetArg(args[argcnt], XmNlabelString, st1=MKSTRING("Find")); argcnt++;
XtSetArg(args[argcnt], XmNshowAsDefault, (short)1); argcnt++;
XtSetArg(args[argcnt], XmNtopAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNleftAttachment, XmATTACH_POSITION); argcnt++;
XtSetArg(args[argcnt], XmNrightAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNbottomAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNleftPosition, 20); argcnt++;
XtSetArg(args[argcnt], XmNbottomOffset, 6); argcnt++;
findBtn = XmCreatePushButton(btnForm, "find", args, argcnt);
XtAddCallback(findBtn, XmNactivateCallback, (XtCallbackProc)findCB, window);
XmStringFree(st1);
XtManageChild(findBtn);
XtVaGetValues(findBtn, XmNshadowThickness, &shadowThickness, 0);
defaultBtnOffset = shadowThickness + 4;
argcnt = 0;
XtSetArg(args[argcnt], XmNtraversalOn, True); argcnt++;
XtSetArg(args[argcnt], XmNhighlightThickness, 2); argcnt++;
XtSetArg(args[argcnt], XmNlabelString, st1=MKSTRING("Cancel")); argcnt++;
XtSetArg(args[argcnt], XmNtopAttachment, XmATTACH_FORM); argcnt++;
XtSetArg(args[argcnt], XmNbottomAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNleftAttachment, XmATTACH_NONE); argcnt++;
XtSetArg(args[argcnt], XmNrightAttachment, XmATTACH_POSITION); argcnt++;
XtSetArg(args[argcnt], XmNrightPosition, 80); argcnt++;
XtSetArg(args[argcnt], XmNtopOffset, defaultBtnOffset); argcnt++;
cancelBtn = XmCreatePushButton(btnForm, "cancel", args, argcnt);
XtAddCallback(cancelBtn, XmNactivateCallback, (XtCallbackProc)fCancelCB,
window);
XmStringFree(st1);
XtManageChild(cancelBtn);
XtVaSetValues(form, XmNcancelButton, cancelBtn, 0);
AddDialogMnemonicHandler(form);
window->findDlog = form;
window->findText = findText;
window->findLiteralBtn = literalBtn;
window->findCaseBtn = caseBtn;
window->findRegExpBtn = regExpBtn;
window->findFwdBtn = forwardBtn;
window->findRevBtn = reverseBtn;
window->findKeepBtn = keepBtn;
window->findBtns = btnForm;
window->findBtn = findBtn;
window->findSearchTypeBox = searchTypeBox;
}
/*
** These callbacks fix a Motif 1.1 problem that the default button gets the
** keyboard focus when a dialog is created. We want the first text field
** to get the focus, so we don't set the default button until the text field
** has the focus for sure. I have tried many other ways and this is by far
** the least nasty.
*/
static void fFocusCB(Widget w, WindowInfo *window, caddr_t *callData)
{
SET_ONE_RSRC(window->findDlog, XmNdefaultButton, window->findBtn);
}
static void rFocusCB(Widget w, WindowInfo *window, caddr_t *callData)
{
SET_ONE_RSRC(window->replaceDlog, XmNdefaultButton, window->replaceBtn);
}
/* when keeping a window up, clue the user what window it's associated with */
static void rKeepCB(Widget w, WindowInfo *window, caddr_t *callData)
{
char title[MAXPATHLEN + 14];
if (XmToggleButtonGetState(w)) {
sprintf(title, "Replace (in %s)", window->filename);
XtVaSetValues(XtParent(window->replaceDlog), XmNtitle, title, 0);
} else
XtVaSetValues(XtParent(window->replaceDlog), XmNtitle, "Replace", 0);
}
static void fKeepCB(Widget w, WindowInfo *window, caddr_t *callData)
{
char title[MAXPATHLEN + 11];
if (XmToggleButtonGetState(w)) {
sprintf(title, "Find (in %s)", window->filename);
XtVaSetValues(XtParent(window->findDlog), XmNtitle, title, 0);
} else
XtVaSetValues(XtParent(window->findDlog), XmNtitle, "Find", 0);
}
static void replaceCB(Widget w, WindowInfo *window,
XmAnyCallbackStruct *callData)
{
char searchString[SEARCHMAX], replaceString[SEARCHMAX];
int direction, searchType;
char *params[4];
/* Validate and fetch the find and replace strings from the dialog */
if (!getReplaceDlogInfo(window, &direction, searchString, replaceString,
&searchType))
return;
/* Set the initial focus of the dialog back to the search string */
resetReplaceTabGroup(window);
/* Find the text and replace it */
params[0] = searchString;
params[1] = replaceString;
params[2] = directionArg(direction);
params[3] = searchTypeArg(searchType);
XtCallActionProc(window->lastFocus, "replace", callData->event, params, 4);
/* Pop down the dialog */
if (!XmToggleButtonGetState(window->replaceKeepBtn))
XtUnmanageChild(window->replaceDlog);
}
static void replaceAllCB(Widget w, WindowInfo *window,
XmAnyCallbackStruct *callData)
{
char searchString[SEARCHMAX], replaceString[SEARCHMAX];
int direction, searchType;
char *params[3];
/* Validate and fetch the find and replace strings from the dialog */
if (!getReplaceDlogInfo(window, &direction, searchString, replaceString,
&searchType))
return;
/* Set the initial focus of the dialog back to the search string */
resetReplaceTabGroup(window);
/* do replacement */
params[0] = searchString;
params[1] = replaceString;
params[2] = searchTypeArg(searchType);
XtCallActionProc(window->lastFocus, "replace_all", callData->event,
params, 3);
/* pop down the dialog */
if (!XmToggleButtonGetState(window->replaceKeepBtn))
XtUnmanageChild(window->replaceDlog);
}
static void rInSelCB(Widget w, WindowInfo *window,
XmAnyCallbackStruct *callData)
{
char searchString[SEARCHMAX], replaceString[SEARCHMAX];
int direction, searchType;
char *params[3];
/* Validate and fetch the find and replace strings from the dialog */
if (!getReplaceDlogInfo(window, &direction, searchString, replaceString,
&searchType))
return;
/* Set the initial focus of the dialog back to the search string */
resetReplaceTabGroup(window);
/* do replacement */
params[0] = searchString;
params[1] = replaceString;
params[2] = searchTypeArg(searchType);
XtCallActionProc(window->lastFocus, "replace_in_selection",
callData->event, params, 3);
/* pop down the dialog */
if (!XmToggleButtonGetState(window->replaceKeepBtn))
XtUnmanageChild(window->replaceDlog);
}
static void rCancelCB(Widget w, WindowInfo *window, caddr_t callData)
{
/* Set the initial focus of the dialog back to the search string */
resetReplaceTabGroup(window);
/* pop down the dialog */
XtUnmanageChild(window->replaceDlog);
}
static void fCancelCB(Widget w, WindowInfo *window, caddr_t callData)
{
/* Set the initial focus of the dialog back to the search string */
resetFindTabGroup(window);
/* pop down the dialog */
XtUnmanageChild(window->findDlog);
}
static void rFindCB(Widget w, WindowInfo *window,XmAnyCallbackStruct *callData)
{
char searchString[SEARCHMAX], replaceString[SEARCHMAX];
int direction, searchType;
char *params[3];
/* Validate and fetch the find and replace strings from the dialog */
if (!getReplaceDlogInfo(window, &direction, searchString, replaceString,
&searchType))
return;
/* Set the initial focus of the dialog back to the search string */
resetReplaceTabGroup(window);
/* Find the text and mark it */
params[0] = searchString;
params[1] = directionArg(direction);
params[2] = searchTypeArg(searchType);
XtCallActionProc(window->lastFocus, "find", callData->event, params, 3);
/* Doctor the search history generated by the action to include the
replace string (if any), so the replace string can be used on
subsequent replaces, even though no actual replacement was done. */
if (!strcmp(SearchHistory[historyIndex(1)], searchString)) {
XtFree(ReplaceHistory[historyIndex(1)]);
ReplaceHistory[historyIndex(1)] = XtNewString(replaceString);
}
/* Pop down the dialog */
if (!XmToggleButtonGetState(window->replaceKeepBtn))
XtUnmanageChild(window->replaceDlog);
}
static void rFindArrowKeyCB(Widget w, WindowInfo *window, XKeyEvent *event)
{
KeySym keysym = XLookupKeysym(event, 0);
int index = window->rHistIndex;
char *searchStr, *replaceStr;
int searchType;
Widget button;
/* only process up and down arrow keys */
if (keysym != XK_Up && keysym != XK_Down)
return;
/* increment or decrement the index depending on which arrow was pressed */
index += (keysym == XK_Up) ? 1 : -1;
/* if the index is out of range, beep and return */
if (index != 0 && historyIndex(index) == -1) {
XBell(TheDisplay, 0);
return;
}
/* determine the strings and button settings to use */
if (index == 0) {
searchStr = "";
replaceStr = "";
searchType = GetPrefSearch();
} else {
searchStr = SearchHistory[historyIndex(index)];
replaceStr = ReplaceHistory[historyIndex(index)];
searchType = SearchTypeHistory[historyIndex(index)];
}
/* Set the buttons and fields with the selected search type */
switch (searchType) {
case SEARCH_LITERAL:
button = window->replaceLiteralBtn;
break;
case SEARCH_CASE_SENSE:
button = window->replaceCaseBtn;
break;
case SEARCH_REGEX:
button = window->replaceRegExpBtn;
break;
}
XmToggleButtonSetState(button, True, True);
XmTextSetString(window->replaceText, searchStr);
XmTextSetString(window->replaceWithText, replaceStr);
window->rHistIndex = index;
}
static void replaceArrowKeyCB(Widget w, WindowInfo *window, XKeyEvent *event)
{
KeySym keysym = XLookupKeysym(event, 0);
int index = window->rHistIndex;
/* only process up and down arrow keys */
if (keysym != XK_Up && keysym != XK_Down)
return;
/* increment or decrement the index depending on which arrow was pressed */
index += (keysym == XK_Up) ? 1 : -1;
/* if the index is out of range, beep and return */
if (index != 0 && historyIndex(index) == -1) {
XBell(TheDisplay, 0);
return;
}
/* change only the replace field information */
if (index == 0)
XmTextSetString(window->replaceWithText, "");
else
XmTextSetString(window->replaceWithText,
ReplaceHistory[historyIndex(index)]);
window->rHistIndex = index;
}
static void findArrowKeyCB(Widget w, WindowInfo *window, XKeyEvent *event)
{
KeySym keysym = XLookupKeysym(event, 0);
int index = window->fHistIndex;
char *searchStr;
int searchType;
Widget button;
/* only process up and down arrow keys */
if (keysym != XK_Up && keysym != XK_Down)
return;
/* increment or decrement the index depending on which arrow was pressed */
index += (keysym == XK_Up) ? 1 : -1;
/* if the index is out of range, beep and return */
if (index != 0 && historyIndex(index) == -1) {
XBell(TheDisplay, 0);
return;
}
/* determine the strings and button settings to use */
if (index == 0) {
searchStr = "";
searchType = GetPrefSearch();
} else {
searchStr = SearchHistory[historyIndex(index)];
searchType = SearchTypeHistory[historyIndex(index)];
}
/* Set the buttons and fields with the selected search type */
switch (searchType) {
case SEARCH_LITERAL:
button = window->findLiteralBtn;
break;
case SEARCH_CASE_SENSE:
button = window->findCaseBtn;
break;
case SEARCH_REGEX:
button = window->findRegExpBtn;
break;
}
XmToggleButtonSetState(button, True, True);
XmTextSetString(window->findText, searchStr);
window->fHistIndex = index;
}
static void findCB(Widget w, WindowInfo *window,XmAnyCallbackStruct *callData)
{
char searchString[SEARCHMAX];
int direction, searchType;
char *params[3];
/* fetch find string, direction and type from the dialog */
if (!getFindDlogInfo(window, &direction, searchString, &searchType))
return;
/* Set the initial focus of the dialog back to the search string */
resetFindTabGroup(window);
/* find the text and mark it */
params[0] = searchString;
params[1] = directionArg(direction);
params[2] = searchTypeArg(searchType);
XtCallActionProc(window->lastFocus, "find", callData->event, params, 3);
/* pop down the dialog */
if (!XmToggleButtonGetState(window->findKeepBtn))
XtUnmanageChild(window->findDlog);
}
/*
** Fetch and verify (particularly regular expression) search and replace
** strings and search type from the Replace dialog. If the strings are ok,
** save a copy in the search history, copy them in to "searchString",
** "replaceString', which are assumed to be at least SEARCHMAX in length,
** return search type in "searchType", and return TRUE as the function
** value. Otherwise, return FALSE.
*/
static int getReplaceDlogInfo(WindowInfo *window, int *direction,
char *searchString, char *replaceString, int *searchType)
{
char *replaceText, *replaceWithText;
regexp *compiledRE = NULL;
char *compileMsg;
int type;
/* Get the search and replace strings, search type, and direction
from the dialog */
replaceText = XmTextGetString(window->replaceText);
replaceWithText = XmTextGetString(window->replaceWithText);
if (XmToggleButtonGetState(window->replaceLiteralBtn))
type = SEARCH_LITERAL;
else if (XmToggleButtonGetState(window->replaceCaseBtn))
type = SEARCH_CASE_SENSE;
else
type = SEARCH_REGEX;
*direction = XmToggleButtonGetState(window->replaceFwdBtn) ? SEARCH_FORWARD :
SEARCH_BACKWARD;
*searchType = type;
/* If the search type is a regular expression, test compile it immediately
and present error messages */
if (type == SEARCH_REGEX) {
compiledRE = CompileRE(replaceText, &compileMsg);
if (compiledRE == NULL) {
DialogF(DF_WARN, XtParent(window->replaceDlog), 1,
"Please respecify the search string:\n%s", "OK", compileMsg);
return FALSE;
}
}
/* Return strings */
strcpy(searchString, replaceText);
strcpy(replaceString, replaceWithText);
XtFree(replaceText);
XtFree(replaceWithText);
return TRUE;
}
/*
** Fetch and verify (particularly regular expression) search string,
** direction, and search type from the Find dialog. If the search string
** is ok, save a copy in the search history, copy it to "searchString",
** which is assumed to be at least SEARCHMAX in length, return search type
** in "searchType", and return TRUE as the function value. Otherwise,
** return FALSE.
*/
static int getFindDlogInfo(WindowInfo *window, int *direction,
char *searchString, int *searchType)
{
char *findText;
regexp *compiledRE = NULL;
char *compileMsg;
int type;
/* Get the search string, search type, and direction from the dialog */
findText = XmTextGetString(window->findText);
if (XmToggleButtonGetState(window->findLiteralBtn))
type = SEARCH_LITERAL;
else if (XmToggleButtonGetState(window->findCaseBtn))
type = SEARCH_CASE_SENSE;
else
type = SEARCH_REGEX;
*direction = XmToggleButtonGetState(window->findFwdBtn) ? SEARCH_FORWARD :
SEARCH_BACKWARD;
*searchType = type;
/* If the search type is a regular expression, test compile it immediately
and present error messages */
if (type == SEARCH_REGEX) {
compiledRE = CompileRE(findText, &compileMsg);
if (compiledRE == NULL) {
DialogF(DF_WARN, XtParent(window->findDlog), 1,
"Please respecify the search string:\n%s", "OK", compileMsg);
return FALSE;
}
XtFree((char *)compiledRE);
}
/* Return the search string */
strcpy(searchString, findText);
XtFree(findText);
return TRUE;
}
int SearchAndSelectSame(WindowInfo *window, int direction)
{
if (NHist < 1) {
XBell(TheDisplay, 0);
return FALSE;
}
return SearchAndSelect(window, direction, SearchHistory[historyIndex(1)],
SearchTypeHistory[historyIndex(1)]);
}
/*
** Search for "searchString" in "window", and select the matching text in
** the window when found (or beep or put up a dialog if not found). Also
** adds the search string to the global search history.
*/
int SearchAndSelect(WindowInfo *window, int direction, char *searchString,
int searchType)
{
int startPos, endPos;
int beginPos, cursorPos, selStart, selEnd;
/* Save a copy of searchString in the search history */
saveSearchHistory(searchString, NULL, searchType);
/* set the position to start the search so we don't find the same
string that was found on the last search */
if (searchMatchesSelection(window, searchString, searchType,
&selStart, &selEnd)) {
/* selection matches search string, start before or after sel. */
if (direction == SEARCH_BACKWARD) {
beginPos = selStart-1;
} else {
beginPos = selEnd;
}
} else {
selStart = -1; selEnd = -1;
/* no selection, or no match, search relative cursor */
cursorPos = TextGetCursorPos(window->lastFocus);
if (direction == SEARCH_BACKWARD) {
/* use the insert position - 1 for backward searches */
beginPos = cursorPos-1;
} else {
/* use the insert position for forward searches */
beginPos = cursorPos;
}
}
/* do the search. SearchWindow does appropriate dialogs and beeps */
if (!SearchWindow(window, direction, searchString, searchType,
beginPos, &startPos, &endPos))
return FALSE;
/* if the search matched an empty string (possible with regular exps)
beginning at the start of the search, go to the next occurrence,
otherwise repeated finds will get "stuck" at zero-length matches */
if (direction==SEARCH_FORWARD && beginPos==startPos && beginPos==endPos)
if (!SearchWindow(window, direction, searchString, searchType,
beginPos+1, &startPos, &endPos))
return FALSE;
/* if matched text is already selected, just beep */
if (selStart==startPos && selEnd==endPos) {
XBell(TheDisplay, 0);
return FALSE;
}
/* select the text found string */
BufSelect(window->buffer, startPos, endPos);
MakeSelectionVisible(window, window->lastFocus);
TextSetCursorPos(window->lastFocus, endPos);
return TRUE;
}
void SearchForSelected(WindowInfo *window, int direction, Time time)
{
XtGetSelectionValue(window->textArea, XA_PRIMARY, XA_STRING,
(XtSelectionCallbackProc)selectedSearchCB, (XtPointer)direction,
time);
}
static void selectedSearchCB(Widget w, XtPointer callData, Atom *selection,
Atom *type, char *value, int *length, int *format)
{
WindowInfo *window = WidgetToWindow(w);
int direction = (int)callData;
int searchType;
char searchString[SEARCHMAX+1];
/* skip if we can't get the selection data or it's too long */
if (*type == XT_CONVERT_FAIL || value == NULL) {
if (GetPrefSearchDlogs())
DialogF(DF_WARN, window->shell, 1,
"Selection not appropriate for searching", "OK");
else
XBell(TheDisplay, 0);
return;
}
if (*length > SEARCHMAX) {
if (GetPrefSearchDlogs())
DialogF(DF_WARN, window->shell, 1, "Selection too long", "OK");
else
XBell(TheDisplay, 0);
XtFree(value);
return;
}
if (*length == 0) {
XBell(TheDisplay, 0);
XtFree(value);
return;
}
/* should be of type text??? */
if (*format != 8) {
fprintf(stderr, "NEdit: can't handle non 8-bit text\n");
XBell(TheDisplay, 0);
XtFree(value);
return;
}
/* make the selection the current search string */
strncpy(searchString, value, *length);
searchString[*length] = '\0';
XtFree(value);
/* Use the default method for searching, unless it is regex, since this
kind of search is by definition a literal search */
searchType = GetPrefSearch();
if (searchType == SEARCH_REGEX)
searchType = SEARCH_LITERAL;
/* search for it in the window */
SearchAndSelect(window, direction, searchString, searchType);
}
/*
** Check the character before the insertion cursor of textW and flash
** matching parenthesis, brackets, or braces, by temporarily highlighting
** the matching character (a timer procedure is scheduled for removing the
** highlights)
*/
void FlashMatching(WindowInfo *window, Widget textW)
{
char c;
int pos, matchIndex;
int startPos, endPos, searchPos, matchPos;
/* if a marker is already drawn, erase it and cancel the timeout */
if (window->flashTimeoutID != 0) {
eraseFlash(window);
XtRemoveTimeOut(window->flashTimeoutID);
window->flashTimeoutID = 0;
}
/* don't do anything if showMatching isn't on */
if (!window->showMatching)
return;
/* don't flash matching characters if there's a selection */
if (window->buffer->primary.selected)
return;
/* get the character to match and the position to start from */
pos = TextGetCursorPos(textW) - 1;
if (pos < 0)
return;
c = BufGetCharacter(window->buffer, pos);
/* is the character one we want to flash? */
for (matchIndex = 0; matchIndex<N_FLASH_CHARS; matchIndex++) {
if (MatchingChars[matchIndex].c == c)
break;
}
if (matchIndex == N_FLASH_CHARS)
return;
/* Constrain the search to visible text (unless we're in split-window mode,
then search the whole buffer), and get the string to search */
if (MatchingChars[matchIndex].direction == SEARCH_BACKWARD) {
startPos = window->nPanes == 0 ? TextFirstVisiblePos(textW) : 0;
endPos = pos;
searchPos = endPos;
} else {
startPos = pos;
endPos = window->nPanes == 0 ? TextLastVisiblePos(textW) :
window->buffer->length;
searchPos = startPos;
}
/* do the search */
if (!findMatchingChar(window->buffer, c, searchPos, startPos, endPos,
&matchPos))
return;
/* highlight the matched character */
BufHighlight(window->buffer, matchPos, matchPos+1);
/* Set up a timer to erase the box after 1.5 seconds */
window->flashTimeoutID = XtAppAddTimeOut(
XtWidgetToApplicationContext(window->shell), 1500,
flashTimeoutProc, window);
window->flashPos = matchPos;
}
void MatchSelectedCharacter(WindowInfo *window)
{
int selStart, selEnd;
int startPos, endPos, matchPos;
textBuffer *buf = window->buffer;
/* get the character to match and its position from the selection, or
the character before the insert point if nothing is selected.
Give up if too many characters are selected */
if (!GetSimpleSelection(buf, &selStart, &selEnd)) {
selEnd = TextGetCursorPos(window->lastFocus);
if (window->overstrike)
selEnd += 1;
selStart = selEnd - 1;
if (selStart < 0) {
XBell(TheDisplay, 0);
return;
}
}
if ((selEnd - selStart) != 1) {
XBell(TheDisplay, 0);
return;
}
/* Search for it in the buffer */
if (!findMatchingChar(buf, BufGetCharacter(buf, selStart), selStart, 0,
buf->length, &matchPos)) {
XBell(TheDisplay, 0);
return;
}
startPos = (matchPos > selStart) ? selStart : matchPos;
endPos = (matchPos > selStart) ? matchPos : selStart;
/* select the text between the matching characters */
BufSelect(buf, startPos, endPos+1);
}
static int findMatchingChar(textBuffer *buf, char toMatch, int charPos,
int startLimit, int endLimit, int *matchPos)
{
int nestDepth, matchIndex, direction, beginPos, pos;
char matchChar, c;
/* Look up the matching character and match direction */
for (matchIndex = 0; matchIndex<N_MATCH_CHARS; matchIndex++) {
if (MatchingChars[matchIndex].c == toMatch)
break;
}
if (matchIndex == N_MATCH_CHARS)
return FALSE;
matchChar = MatchingChars[matchIndex].match;
direction = MatchingChars[matchIndex].direction;
/* find it in the buffer */
beginPos = (direction==SEARCH_FORWARD) ? charPos+1 : charPos-1;
nestDepth = 1;
if (direction == SEARCH_FORWARD) {
for (pos=beginPos; pos<endLimit; pos++) {
c=BufGetCharacter(buf, pos);
if (c == matchChar) {
nestDepth--;
if (nestDepth == 0) {
*matchPos = pos;
return TRUE;
}
} else if (c == toMatch)
nestDepth++;
}
} else { /* SEARCH_BACKWARD */
for (pos=beginPos; pos>=startLimit; pos--) {
c=BufGetCharacter(buf, pos);
if (c == matchChar) {
nestDepth--;
if (nestDepth == 0) {
*matchPos = pos;
return TRUE;
}
} else if (c == toMatch)
nestDepth++;
}
}
return FALSE;
}
/*
** Xt timer procedure for erasing the matching parenthesis marker.
*/
static void flashTimeoutProc(XtPointer clientData, XtIntervalId *id)
{
eraseFlash((WindowInfo *)clientData);
((WindowInfo *)clientData)->flashTimeoutID = 0;
}
/*
** Erase the marker drawn on a matching parenthesis bracket or brace
** character.
*/
static void eraseFlash(WindowInfo *window)
{
BufUnhighlight(window->buffer);
}
/*
** Search and replace using previously entered search strings (from dialog
** or selection).
*/
int ReplaceSame(WindowInfo *window, int direction)
{
if (NHist < 1) {
XBell(TheDisplay, 0);
return FALSE;
}
return SearchAndReplace(window, direction, SearchHistory[historyIndex(1)],
ReplaceHistory[historyIndex(1)],
SearchTypeHistory[historyIndex(1)]);
}
/*
** Search for string "searchString" in window "window", using algorithm
** "searchType" and direction "direction", and replace it with "replaceString"
** Also adds the search and replace strings to the global search history.
*/
int SearchAndReplace(WindowInfo *window, int direction, char *searchString,
char *replaceString, int searchType)
{
int startPos, endPos, replaceLen;
int found;
int beginPos, cursorPos;
/* Save a copy of search and replace strings in the search history */
saveSearchHistory(searchString, replaceString, searchType);
/* If the text selected in the window matches the search string, */
/* the user is probably using search then replace method, so */
/* replace the selected text regardless of where the cursor is. */
/* Otherwise, search for the string. */
if (!searchMatchesSelection(window, searchString, searchType,
&startPos, &endPos)) {
/* get the position to start the search */
cursorPos = TextGetCursorPos(window->lastFocus);
if (direction == SEARCH_BACKWARD) {
/* use the insert position - 1 for backward searches */
beginPos = cursorPos-1;
} else {
/* use the insert position for forward searches */
beginPos = cursorPos;
}
/* do the search */
found = SearchWindow(window, direction, searchString, searchType,
beginPos, &startPos, &endPos);
if (!found)
return FALSE;
}
/* replace the text */
if (searchType == SEARCH_REGEX) {
char replaceResult[SEARCHMAX], *foundString;
foundString = BufGetRange(window->buffer, startPos, endPos);
replaceUsingRE(searchString, replaceString, foundString,
replaceResult, SEARCHMAX, GetWindowDelimiters(window));
XtFree(foundString);
BufReplace(window->buffer, startPos, endPos, replaceResult);
replaceLen = strlen(replaceResult);
} else {
BufReplace(window->buffer, startPos, endPos, replaceString);
replaceLen = strlen(replaceString);
}
/* after successfully completing a replace, selected text attracts
attention away from the area of the replacement, particularly
when the selection represents a previous search. so deselect */
BufUnselect(window->buffer);
/* temporarily shut off autoShowInsertPos before setting the cursor
position so MakeSelectionVisible gets a chance to place the replaced
string at a pleasing position on the screen (otherwise, the cursor would
be automatically scrolled on screen and MakeSelectionVisible would do
nothing) */
XtVaSetValues(window->lastFocus, textNautoShowInsertPos, False, 0);
TextSetCursorPos(window->lastFocus, startPos +
((direction == SEARCH_FORWARD) ? replaceLen : 0));
MakeSelectionVisible(window, window->lastFocus);
XtVaSetValues(window->lastFocus, textNautoShowInsertPos, True, 0);
return TRUE;
}
/*
** Replace all occurences of "searchString" in "window" with "replaceString"
** within the current primary selection in "window". Also adds the search and
** replace strings to the global search history.
*/
int ReplaceInSelection(WindowInfo *window, char *searchString,
char *replaceString, int searchType)
{
int selStart, selEnd, beginPos, startPos, endPos, realOffset, replaceLen;
int found, anyFound, isRect, rectStart, rectEnd, lineStart, cursorPos;
char *fileString;
textBuffer *tempBuf;
/* save a copy of search and replace strings in the search history */
saveSearchHistory(searchString, replaceString, searchType);
/* find out where the selection is */
if (!BufGetSelectionPos(window->buffer, &selStart, &selEnd, &isRect,
&rectStart, &rectEnd))
return FALSE;
/* get the selected text */
if (isRect) {
selStart = BufStartOfLine(window->buffer, selStart);
selEnd = BufEndOfLine(window->buffer, selEnd);
fileString = BufGetRange(window->buffer, selStart, selEnd);
} else
fileString = BufGetSelectionText(window->buffer);
/* create a temporary buffer in which to do the replacements to hide the
intermediate steps from the display routines, and so everything can
be undone in a single operation */
tempBuf = BufCreate();
BufSetAll(tempBuf, fileString);
/* search the string and do the replacements in the temporary buffer */
replaceLen = strlen(replaceString);
found = TRUE;
anyFound = FALSE;
beginPos = 0;
realOffset = 0;
while (found) {
found = SearchString(fileString, searchString, SEARCH_FORWARD,
searchType, FALSE, beginPos, &startPos, &endPos,
GetWindowDelimiters(window));
if (!found)
break;
/* if the selection is rectangular, verify that the found
string is in the rectangle */
if (isRect) {
lineStart = BufStartOfLine(tempBuf, startPos+realOffset);
if (BufCountDispChars(tempBuf, lineStart, startPos+realOffset) <
rectStart || BufCountDispChars(tempBuf, lineStart,
endPos+realOffset) > rectEnd) {
beginPos = (startPos == endPos) ? endPos+1 : endPos;
continue;
}
}
/* Make sure the match did not start past the end (regular expressions
can consider the artificial end of the range as the end of a line,
and match a fictional whole line beginning there) */
if (startPos == selEnd - selStart) {
found = False;
break;
}
/* replace the string and compensate for length change */
if (searchType == SEARCH_REGEX) {
char replaceResult[SEARCHMAX], *foundString;
foundString = BufGetRange(tempBuf, startPos+realOffset,
endPos+realOffset);
replaceUsingRE(searchString, replaceString, foundString,
replaceResult, SEARCHMAX, GetWindowDelimiters(window));
XtFree(foundString);
BufReplace(tempBuf, startPos+realOffset, endPos+realOffset,
replaceResult);
replaceLen = strlen(replaceResult);
} else
BufReplace(tempBuf, startPos+realOffset, endPos+realOffset,
replaceString);
realOffset += replaceLen - (endPos - startPos);
/* start again after match unless match was empty, then endPos+1 */
beginPos = (startPos == endPos) ? endPos+1 : endPos;
cursorPos = endPos;
anyFound = TRUE;
}
XtFree(fileString);
/* if nothing was found, tell user and return */
if (!anyFound) {
if (GetPrefSearchDlogs()) {
/* Avoid bug in Motif 1.1 by putting away search dialog
before DialogF */
if (window->findDlog && XtIsManaged(window->findDlog) &&
!XmToggleButtonGetState(window->findKeepBtn))
XtUnmanageChild(window->findDlog);
if (window->replaceDlog && XtIsManaged(window->replaceDlog) &&
!XmToggleButtonGetState(window->replaceKeepBtn))
XtUnmanageChild(window->replaceDlog);
DialogF(DF_INF, window->shell, 1, "String was not found", "OK");
} else
XBell(TheDisplay, 0);
BufFree(tempBuf);
return FALSE;
}
/* replace the selected range in the real buffer */
fileString = BufGetAll(tempBuf);
BufFree(tempBuf);
BufReplace(window->buffer, selStart, selEnd, fileString);
XtFree(fileString);
/* set the insert point at the end of the last replacement */
TextSetCursorPos(window->lastFocus, selStart + cursorPos + realOffset);
/* leave non-rectangular selections selected (rect. ones after replacement
are less useful since left/right positions are randomly adjusted) */
if (!isRect)
BufSelect(window->buffer, selStart, selEnd + realOffset);
return TRUE;
}
/*
** Replace all occurences of "searchString" in "window" with "replaceString".
** Also adds the search and replace strings to the global search history.
*/
int ReplaceAll(WindowInfo *window, char *searchString, char *replaceString,
int searchType)
{
char *fileString, *newFileString;
int copyStart, copyEnd, replacementLen;
/* reject empty string */
if (*searchString == '\0')
return FALSE;
/* save a copy of search and replace strings in the search history */
saveSearchHistory(searchString, replaceString, searchType);
/* get the entire text buffer from the text area widget */
fileString = BufGetAll(window->buffer);
newFileString = ReplaceAllInString(fileString, searchString, replaceString,
searchType, ©Start, ©End, &replacementLen,
GetWindowDelimiters(window));
if (newFileString == NULL) {
if (GetPrefSearchDlogs()) {
if (window->findDlog && XtIsManaged(window->findDlog) &&
!XmToggleButtonGetState(window->findKeepBtn))
XtUnmanageChild(window->findDlog);
if (window->replaceDlog && XtIsManaged(window->replaceDlog) &&
!XmToggleButtonGetState(window->replaceKeepBtn))
XtUnmanageChild(window->replaceDlog);
DialogF(DF_INF, window->shell, 1, "String was not found", "OK");
} else
XBell(TheDisplay, 0);
return FALSE;
}
XtFree(fileString);
/* replace the contents of the text widget with the substituted text */
BufReplace(window->buffer, copyStart, copyEnd, newFileString);
/* Move the cursor to the end of the last replacement */
TextSetCursorPos(window->lastFocus, copyStart + replacementLen);
XtFree(newFileString);
return TRUE;
}
/*
** Replace all occurences of "searchString" in "inString" with "replaceString"
** and return an allocated string covering the range between the start of the
** first replacement (returned in "copyStart", and the end of the last
** replacement (returned in "copyEnd")
*/
char *ReplaceAllInString(char *inString, char *searchString,
char *replaceString, int searchType, int *copyStart,
int *copyEnd, int *replacementLength, char *delimiters)
{
int beginPos, startPos, endPos, lastEndPos;
int found, nFound, removeLen, replaceLen, copyLen, addLen;
char *outString, *fillPtr;
/* reject empty string */
if (*searchString == '\0')
return NULL;
/* rehearse the search first to determine the size of the buffer needed
to hold the substituted text. No substitution done here yet */
replaceLen = strlen(replaceString);
found = TRUE;
nFound = 0;
removeLen = 0;
addLen = 0;
beginPos = 0;
*copyStart = -1;
while (found) {
found = SearchString(inString, searchString, SEARCH_FORWARD,
searchType, FALSE, beginPos, &startPos, &endPos, delimiters);
if (found) {
if (*copyStart < 0)
*copyStart = startPos;
*copyEnd = endPos;
/* start next after match unless match was empty, then endPos+1 */
beginPos = (startPos == endPos) ? endPos+1 : endPos;
nFound++;
removeLen += endPos - startPos;
if (searchType == SEARCH_REGEX) {
char replaceResult[SEARCHMAX];
replaceUsingRE(searchString, replaceString, &inString[startPos],
replaceResult, SEARCHMAX, delimiters);
addLen += strlen(replaceResult);
} else
addLen += replaceLen;
}
}
if (nFound == 0)
return NULL;
/* Allocate a new buffer to hold all of the new text between the first
and last substitutions */
copyLen = *copyEnd - *copyStart;
outString = XtMalloc(copyLen - removeLen + addLen + 1);
/* Scan through the text buffer again, substituting the replace string
and copying the part between replaced text to the new buffer */
found = TRUE;
beginPos = 0;
lastEndPos = 0;
fillPtr = outString;
while (found) {
found = SearchString(inString, searchString, SEARCH_FORWARD,
searchType, FALSE, beginPos, &startPos, &endPos, delimiters);
if (found) {
if (beginPos != 0) {
memcpy(fillPtr, &inString[lastEndPos], startPos - lastEndPos);
fillPtr += startPos - lastEndPos;
}
if (searchType == SEARCH_REGEX) {
char replaceResult[SEARCHMAX];
replaceUsingRE(searchString, replaceString, &inString[startPos],
replaceResult, SEARCHMAX, delimiters);
replaceLen = strlen(replaceResult);
memcpy(fillPtr, replaceResult, replaceLen);
} else {
memcpy(fillPtr, replaceString, replaceLen);
}
fillPtr += replaceLen;
lastEndPos = endPos;
/* start next after match unless match was empty, then endPos+1 */
beginPos = (startPos == endPos) ? endPos+1 : endPos;
}
}
*fillPtr = '\0';
*replacementLength = fillPtr - outString;
return outString;
}
/*
** Search the text in "window", attempting to match "searchString"
*/
int SearchWindow(WindowInfo *window, int direction, char *searchString,
int searchType, int beginPos, int *startPos, int *endPos)
{
char *fileString;
int found, resp, fileEnd;
/* reject empty string */
if (*searchString == '\0')
return FALSE;
/* get the entire text buffer from the text area widget */
fileString = BufGetAll(window->buffer);
/* search the string copied from the text area widget, and present
dialogs, or just beep */
if (GetPrefSearchDlogs()) {
found = SearchString(fileString, searchString, direction, searchType,
FALSE, beginPos, startPos, endPos, GetWindowDelimiters(window));
/* Avoid Motif 1.1 bug by putting away search dialog before DialogF */
if (window->findDlog && XtIsManaged(window->findDlog) &&
!XmToggleButtonGetState(window->findKeepBtn))
XtUnmanageChild(window->findDlog);
if (window->replaceDlog && XtIsManaged(window->replaceDlog) &&
!XmToggleButtonGetState(window->replaceKeepBtn))
XtUnmanageChild(window->replaceDlog);
if (!found) {
fileEnd = window->buffer->length - 1;
if (direction == SEARCH_FORWARD && beginPos != 0) {
resp = DialogF(DF_QUES, window->shell, 2,
"Continue search from\nbeginning of file?", "Continue",
"Cancel");
if (resp == 2) {
XtFree(fileString);
return False;
}
found = SearchString(fileString, searchString, direction,
searchType, FALSE, 0, startPos, endPos,
GetWindowDelimiters(window));
} else if (direction == SEARCH_BACKWARD && beginPos != fileEnd) {
resp = DialogF(DF_QUES, window->shell, 2,
"Continue search\nfrom end of file?", "Continue",
"Cancel");
if (resp == 2) {
XtFree(fileString);
return False;
}
found = SearchString(fileString, searchString, direction,
searchType, FALSE, fileEnd, startPos, endPos,
GetWindowDelimiters(window));
}
if (!found)
DialogF(DF_INF, window->shell,1,"String was not found","OK");
}
} else { /* no dialogs */
found = SearchString(fileString, searchString, direction,
searchType, TRUE, beginPos, startPos, endPos,
GetWindowDelimiters(window));
if (!found)
XBell(TheDisplay, 0);
}
/* Free the text buffer copy returned from BufGetAll */
XtFree(fileString);
return found;
}
/*
** Search the null terminated string "string" for "searchString", beginning at
** "beginPos". Returns the boundaries of the match in "startPos" and "endPos".
** "delimiters" may be used to provide an alternative set of word delimiters
** for regular expression "<" and ">" characters, or simply passed as null
** for the default delimiter set.
*/
int SearchString(char *string, char *searchString, int direction,
int searchType, int wrap, int beginPos, int *startPos,
int *endPos, char *delimiters)
{
switch (searchType) {
case SEARCH_CASE_SENSE:
return searchLiteral(string, searchString, TRUE, direction, wrap,
beginPos, startPos, endPos);
case SEARCH_LITERAL:
return searchLiteral(string, searchString, FALSE, direction, wrap,
beginPos, startPos, endPos);
case SEARCH_REGEX:
return searchRegex(string, searchString, direction, wrap,
beginPos, startPos, endPos, delimiters);
}
return FALSE; /* never reached, just makes compilers happy */
}
static int searchLiteral(char *string, char *searchString, int caseSense,
int direction, int wrap, int beginPos, int *startPos, int *endPos)
{
/* This is critical code for the speed of searches. */
/* For efficiency, we define the macro DOSEARCH with the guts of the search */
/* routine and repeat it, changing the parameters of the outer loop for the */
/* searching, forwards, backwards, and before and after the begin point */
#define DOSEARCH() \
if (*filePtr == *ucString || *filePtr == *lcString) { \
/* matched first character */ \
ucPtr = ucString; \
lcPtr = lcString; \
tempPtr = filePtr; \
while (*tempPtr == *ucPtr || *tempPtr == *lcPtr) { \
tempPtr++; ucPtr++; lcPtr++; \
if (*ucPtr == 0) { \
/* matched whole string */ \
*startPos = filePtr - string; \
*endPos = tempPtr - string; \
return TRUE; \
} \
} \
} \
register char *filePtr, *tempPtr, *ucPtr, *lcPtr;
char lcString[SEARCHMAX], ucString[SEARCHMAX];
/* SEARCHMAX was fine in the original NEdit, but it should be done away with
now that searching can be done from macros without limits. Returning
search failure here is cheating users. This limit is not documented. */
if (strlen(searchString) >= SEARCHMAX)
return FALSE;
if (caseSense) {
strcpy(ucString, searchString);
strcpy(lcString, searchString);
} else {
upCaseString(ucString, searchString);
downCaseString(lcString, searchString);
}
if (direction == SEARCH_FORWARD) {
/* search from beginPos to end of string */
for (filePtr=string+beginPos; *filePtr!=0; filePtr++) {
DOSEARCH()
}
if (!wrap)
return FALSE;
/* search from start of file to beginPos */
for (filePtr=string; filePtr<=string+beginPos; filePtr++) {
DOSEARCH()
}
return FALSE;
} else {
/* SEARCH_BACKWARD */
/* search from beginPos to start of file. A negative begin pos */
/* says begin searching from the far end of the file */
if (beginPos >= 0) {
for (filePtr=string+beginPos; filePtr>=string; filePtr--) {
DOSEARCH()
}
}
if (!wrap)
return FALSE;
/* search from end of file to beginPos */
/*... this strlen call is extreme inefficiency, but it's not obvious */
/* how to get the text string length from the text widget (under 1.1)*/
for (filePtr=string+strlen(string);
filePtr>=string+beginPos; filePtr--) {
DOSEARCH()
}
return FALSE;
}
}
static int searchRegex(char *string, char *searchString, int direction,
int wrap, int beginPos, int *startPos, int *endPos, char *delimiters)
{
if (direction == SEARCH_FORWARD)
return forwardRegexSearch(string, searchString, wrap,
beginPos, startPos, endPos, delimiters);
else
return backwardRegexSearch(string, searchString, wrap,
beginPos, startPos, endPos, delimiters);
}
static int forwardRegexSearch(char *string, char *searchString, int wrap,
int beginPos, int *startPos, int *endPos, char *delimiters)
{
regexp *compiledRE = NULL;
char *compileMsg;
/* compile the search string for searching with ExecRE. Note that
this does not process errors from compiling the expression. It
assumes that the expression was checked earlier. */
compiledRE = CompileRE(searchString, &compileMsg);
if (compiledRE == NULL)
return FALSE;
/* search from beginPos to end of string */
if (ExecRE(compiledRE, string + beginPos, NULL, FALSE,
isStartOfLine(string, beginPos), isStartOfWord(string, beginPos),
delimiters)) {
*startPos = compiledRE->startp[0] - string;
*endPos = compiledRE->endp[0] - string;
XtFree((char *)compiledRE);
return TRUE;
}
/* if wrap turned off, we're done */
if (!wrap) {
XtFree((char *)compiledRE);
return FALSE;
}
/* search from the beginning of the string to beginPos */
if (ExecRE(compiledRE, string, string + beginPos, FALSE, TRUE, TRUE,
delimiters)) {
*startPos = compiledRE->startp[0] - string;
*endPos = compiledRE->endp[0] - string;
XtFree((char *)compiledRE);
return TRUE;
}
XtFree((char *)compiledRE);
return FALSE;
}
static int backwardRegexSearch(char *string, char *searchString,
int wrap, int beginPos, int *startPos, int *endPos, char *delimiters)
{
regexp *compiledRE = NULL;
char *compileMsg;
int length;
/* compile the search string for searching with ExecRE */
compiledRE = CompileRE(searchString, &compileMsg);
if (compiledRE == NULL)
return FALSE;
/* search from beginPos to start of file. A negative begin pos */
/* says begin searching from the far end of the file. */
if (beginPos >= 0) {
if (ExecRE(compiledRE, string, string + beginPos, TRUE, TRUE, TRUE,
delimiters)) {
*startPos = compiledRE->startp[0] - string;
*endPos = compiledRE->endp[0] - string;
XtFree((char *)compiledRE);
return TRUE;
}
}
/* if wrap turned off, we're done */
if (!wrap && beginPos >= 0) {
XtFree((char *)compiledRE);
return FALSE;
}
/* search from the end of the string to beginPos */
if (beginPos < 0)
beginPos = 0;
length = strlen(string); /* sadly, this means scanning entire string */
if (ExecRE(compiledRE, string + beginPos, string + length, TRUE,
isStartOfLine(string, beginPos), isStartOfWord(string, beginPos),
delimiters)) {
*startPos = compiledRE->startp[0] - string;
*endPos = compiledRE->endp[0] - string;
XtFree((char *)compiledRE);
return TRUE;
}
XtFree((char *)compiledRE);
return FALSE;
}
static void upCaseString(char *outString, char *inString)
{
char *outPtr, *inPtr;
for (outPtr=outString, inPtr=inString; *inPtr!=0; inPtr++, outPtr++) {
*outPtr = toupper((unsigned char)*inPtr);
}
*outPtr = 0;
}
static void downCaseString(char *outString, char *inString)
{
char *outPtr, *inPtr;
for (outPtr=outString, inPtr=inString; *inPtr!=0; inPtr++, outPtr++) {
*outPtr = tolower((unsigned char)*inPtr);
}
*outPtr = 0;
}
/*
** resetFindTabGroup & resetReplaceTabGroup are really gruesome kludges to
** set the keyboard traversal. XmProcessTraversal does not work at
** all on these dialogs. ...It seems to have started working around
** Motif 1.1.2
*/
static void resetFindTabGroup(WindowInfo *window)
{
#ifdef MOTIF10
XmRemoveTabGroup(window->findText);
XmRemoveTabGroup(window->findSearchTypeBox);
XmRemoveTabGroup(window->findBtns);
XmAddTabGroup(window->findText);
XmAddTabGroup(window->findSearchTypeBox);
XmAddTabGroup(window->findBtns);
#endif
XmProcessTraversal(window->findText, XmTRAVERSE_CURRENT);
}
static void resetReplaceTabGroup(WindowInfo *window)
{
#ifdef MOTIF10
XmRemoveTabGroup(window->replaceText);
XmRemoveTabGroup(window->replaceWithText);
XmRemoveTabGroup(window->replaceSearchTypeBox);
XmRemoveTabGroup(window->replaceBtns);
XmAddTabGroup(window->replaceText);
XmAddTabGroup(window->replaceWithText);
XmAddTabGroup(window->replaceSearchTypeBox);
XmAddTabGroup(window->replaceBtns);
#endif
XmProcessTraversal(window->replaceText, XmTRAVERSE_CURRENT);
}
/*
** Return TRUE if "searchString" exactly matches the text in the window's
** current primary selection using search algorithm "searchType". If true,
** also return the position of the selection in "left" and "right".
*/
static int searchMatchesSelection(WindowInfo *window, char *searchString,
int searchType, int *left, int *right)
{
int selLen, selStart, selEnd, startPos, endPos;
char *string;
int found, isRect, rectStart, rectEnd, lineStart;
/* find length of selection, give up on no selection or too long */
if (!BufGetSelectionPos(window->buffer, &selStart, &selEnd, &isRect,
&rectStart, &rectEnd))
return FALSE;
if (selEnd - selStart > SEARCHMAX)
return FALSE;
/* if the selection is rectangular, don't match if it spans lines */
if (isRect) {
lineStart = BufStartOfLine(window->buffer, selStart);
if (lineStart != BufStartOfLine(window->buffer, selEnd))
return FALSE;
}
/* get the selected text */
string = BufGetSelectionText(window->buffer);
if (*string == '\0') {
XtFree(string);
return FALSE;
}
selLen = strlen(string);
/* search for the string in the selection (we are only interested */
/* in an exact match, but the procedure SearchString does important */
/* stuff like applying the correct matching algorithm) */
found = SearchString(string, searchString, SEARCH_FORWARD, searchType,
FALSE, 0, &startPos, &endPos, GetWindowDelimiters(window));
XtFree(string);
/* decide if it is an exact match */
if (!found)
return FALSE;
if (startPos != 0 || endPos != selLen)
return FALSE;
/* return the start and end of the selection */
if (isRect)
GetSimpleSelection(window->buffer, left, right);
else {
*left = selStart;
*right = selEnd;
}
return TRUE;
}
/*
** Substitutes a replace string for a string that was matched using a
** regular expression. This was added later and is very inneficient
** because instead of using the compiled regular expression that was used
** to make the match in the first place, it re-compiles the expression
** and redoes the search on the already-matched string. This allows the
** code to continue using strings to represent the search and replace
** items.
*/
static void replaceUsingRE(char *searchStr, char *replaceStr, char *sourceStr,
char *destStr, int maxDestLen, char *delimiters)
{
regexp *compiledRE;
char *compileMsg;
compiledRE = CompileRE(searchStr, &compileMsg);
ExecRE(compiledRE, sourceStr, NULL, False, True, True, delimiters);
SubstituteRE(compiledRE, replaceStr, destStr, maxDestLen);
XtFree((char *)compiledRE);
}
/*
** Store the search and replace strings, and search type for later recall.
** If replaceString is NULL, duplicate the last replaceString used.
*/
static void saveSearchHistory(char *searchString, char *replaceString,
int searchType)
{
char *sStr, *rStr;
/* If replaceString is NULL, duplicate the last one (if any) */
if (replaceString == NULL)
replaceString = NHist >= 1 ? ReplaceHistory[historyIndex(1)] : "";
/* Compare the current search and replace strings against the saved ones.
If they are identical, don't bother saving */
if (NHist >= 1 && searchType == SearchTypeHistory[historyIndex(1)] &&
!strcmp(SearchHistory[historyIndex(1)], searchString) &&
!strcmp(ReplaceHistory[historyIndex(1)], replaceString)) {
return;
}
/* If there are more than MAX_SEARCH_HISTORY strings saved, recycle
some space, free the entry that's about to be overwritten */
if (NHist == MAX_SEARCH_HISTORY) {
XtFree(SearchHistory[HistStart]);
XtFree(ReplaceHistory[HistStart]);
} else
NHist++;
/* Allocate and copy the search and replace strings and add them to the
circular buffers at HistStart, bump the buffer pointer to next pos. */
sStr = XtMalloc(strlen(searchString) + 1);
rStr = XtMalloc(strlen(replaceString) + 1);
strcpy(sStr, searchString);
strcpy(rStr, replaceString);
SearchHistory[HistStart] = sStr;
ReplaceHistory[HistStart] = rStr;
SearchTypeHistory[HistStart] = searchType;
HistStart++;
if (HistStart >= MAX_SEARCH_HISTORY)
HistStart = 0;
}
/*
** return an index into the circular buffer arrays of history information
** for search strings, given the number of saveSearchHistory cycles back from
** the current time.
*/
static int historyIndex(int nCycles)
{
int index;
if (nCycles > NHist || nCycles <= 0)
return -1;
index = HistStart - nCycles;
if (index < 0)
index = MAX_SEARCH_HISTORY + index;
return index;
}
/*
** Return a pointer to the string describing search type for search action
** routine parameters (see menu.c for processing of action routines)
*/
static char *searchTypeArg(int searchType)
{
if (searchType == SEARCH_LITERAL)
return "literal";
else if (searchType == SEARCH_CASE_SENSE)
return "case";
return "regex";
}
/*
** Return a pointer to the string describing search direction for search action
** routine parameters (see menu.c for processing of action routines)
*/
static char *directionArg(int direction)
{
if (direction == SEARCH_BACKWARD)
return "backward";
return "forward";
}
/*
** Determine if beginPos begins a line or word in string. Note that we should
** be asking the caller to provide this information about the string as a
** whole, but it probably isn't worth all the extra work
*/
static int isStartOfLine(char *string, int beginPos)
{
return beginPos == 0 ? TRUE : string[beginPos-1] == '\n';
}
static int isStartOfWord(char *string, int beginPos)
{
char *d, prevChar;
if (beginPos == 0)
return TRUE;
prevChar = string[beginPos-1];
if (prevChar == ' ' || prevChar == '\t' || prevChar == '\n')
return TRUE;
for (d=GetPrefDelimiters(); *d!='\0'; d++)
if (string[beginPos-1] == *d)
return TRUE;
return FALSE;
}
|