summaryrefslogtreecommitdiff
path: root/Build/source/texk/xdvik/search-internal.c
blob: 719deaa3f0046600249cd1784fd5238021e4d7e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
/*
 * Copyright (c) 2003-2004 the xdvik development team
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * PAUL VOJTA OR ANY OTHER AUTHOR OF THIS SOFTWARE BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

/*
  How dvi search works:

  (1) Map the search string passed by the user from resource.text_encoding
  to UTF-8 (currently only maps ISO_8859-1), and create a regexp from
  the string if needed.

  If case-insensitive matching is activated and regexp search is not
  active, the search string is converted to lowercase. For regexp
  searches, the flag available with regcomp to ignore case is used.
  
  (2) Scan 2 adjacent pages (so that we can also find matches across page
  boundaries), collecting the text into a char *buffer.
  This involves the following steps:

  - Map characters from wide_ubyte to uint32_t encoding (ucs-4),
  using and Adobe names lookup table (for Postscript Type1
  fonts) or font-specific character info (for Metafont fonts).
	
  This step also maps accented glyphs to composite glyphs,
  expands ligatures to separate characters (e.g. `ffi' to
  `f'`f'`i'), and tries to detect word boundaries and line
  breaks.

  If case-insensitive matching is activated, the characters are
  also lowercased in this step.
	
  Optionally, hyphenation can be removed in this step.
	
  Routine: do_char() in dvi-draw.c (must be in dvi_draw.c since
  we need access to drawing-related stuff like `currinf' to get
  information about the current font, position in the DVI file
  etc.).

  - Save the uint32_t characters into a char *buffer, mapping to
  utf-8.
      
  (3) Search the buffer from (2) for the string from (1). If a match
  has been found, the page is scanned again to store the bounding
  box information of the matched text for highlighting it.
*/



#include "xdvi-config.h"
#include "xdvi.h"

#define SWITCH_TO_UTF8 1

#if HAVE_REGEX_H
#include <regex.h>
#endif

#if HAVE_ICONV_H
#include <iconv.h>
#endif

#include <locale.h>

#if USE_LANGINFO
#include <langinfo.h>
#endif

#include <ctype.h>

#include <X11/keysym.h>

#include "dvi-init.h" /* for total_pages */
#include "dvi-draw.h"
#include "search-internal.h"

#include "search-dialog.h"
#include "message-window.h"
#include "statusline.h"
#include "events.h"
#include "encodings.h"
#include "pagesel.h"
#include "events.h"
#include "util.h"
#include "x_util.h"
#include "string-utils.h"
#include "mag.h"
#include "pagehist.h"
#include "translations.h"

#define DEBUG_SEARCH 0
/* #define MATCH_HIGHLIGHT_INVERTED 1 */


/* margins for highlighting the match */
const int BBOX_LOWER_MARGIN = 2;
const int BBOX_UPPER_MARGIN = 1;

#if 0
#define GET_BBOX(bbox, shrink, x, y, w, h)				\
	x = (bbox.ulx / (double)shrink + 0.5) - BBOX_UPPER_MARGIN;	\
	y = (bbox.uly / (double)shrink + 0.5) - BBOX_UPPER_MARGIN;	\
	w = ((bbox.lrx - bbox.ulx) / (double)shrink) + 0.5		\
	    + BBOX_UPPER_MARGIN + BBOX_LOWER_MARGIN + 1;		\
	h = ((bbox.lry - bbox.uly) / (double)shrink) + 0.5		\
	    + BBOX_UPPER_MARGIN + BBOX_LOWER_MARGIN + 2;
#endif /* 0 */
/* use integer arithmetic */
#define GET_BBOX(bbox, shrink, x, y, w, h)				\
	x = (bbox.ulx + shrink / 2) / shrink - BBOX_UPPER_MARGIN;	\
	y = (bbox.uly + shrink / 2) / shrink - BBOX_UPPER_MARGIN;	\
	w = (bbox.lrx - bbox.ulx + shrink - 1) / shrink			\
	    + BBOX_UPPER_MARGIN + BBOX_LOWER_MARGIN + 1;		\
	h = (bbox.lry - bbox.uly + shrink - 1) / shrink			\
	    + BBOX_UPPER_MARGIN + BBOX_LOWER_MARGIN + 2;

/* access from drawing routines */
static struct word_info *m_info = NULL;
static int m_match_page = -1;

/* Force restarting search if user switches pages. E.g. if
   the current match is the last of 3 matches on the page,
   and user re-selects the page, the search should start again
   with match 1. Similarly, if current page is second of two
   pages scanned, and user clicks on first, search shouldn't continue
   on second page but restart on current one.
*/
static Boolean m_changed_page = False;

static Boolean m_highlight_region_changed = True;

void
search_signal_page_changed(void)
{
    m_changed_page = True;
}


static void
reset_info(struct word_info *w_info)
{
    if (w_info == NULL)
	return;

    free(w_info->txt_buf);
    w_info->txt_buf = NULL;
    w_info->txt_buf_size = 0;
    w_info->curr_buf_idx = 0;

    free(w_info->bboxes);
    w_info->bboxes = NULL;
    w_info->bboxes_size = 0;
    w_info->bboxes_idx = 0;

    m_highlight_region_changed = True;
}


static void
generate_highlight_expose(const struct word_info *info)
{
    int x1 = INT_MAX, y1 = INT_MAX, x2 = 0, y2 = 0;
    size_t i;
    for (i = 0; info->bboxes != NULL && i <= info->bboxes_idx && info->bboxes[i].ulx < INT_MAX; i++) {
	if (info->bboxes[i].ulx < x1)
	    x1 = info->bboxes[i].ulx;
	if (info->bboxes[i].uly < y1)
	    y1 = info->bboxes[i].uly;
	if (info->bboxes[i].lrx > x2)
	    x2 = info->bboxes[i].lrx;
	if (info->bboxes[i].lry > y2)
	    y2 = info->bboxes[i].lry;
    }
#if 0
    fprintf(stderr, "region for exposure: %d,%d to %d,%d\n",
	    x1, y1, x2, y2);
#endif /* 0 */
    x1 -= MAX(BBOX_UPPER_MARGIN, BBOX_LOWER_MARGIN) - 2;
    y1 -= MAX(BBOX_UPPER_MARGIN, BBOX_LOWER_MARGIN) - 2;
    x2 += MAX(BBOX_UPPER_MARGIN, BBOX_LOWER_MARGIN) + 2;
    y2 += MAX(BBOX_UPPER_MARGIN, BBOX_LOWER_MARGIN) + 2;
    clearexpose(&mane, x1, y1, x2 - x1, y2 - y1);
}

static void
draw_bboxes(const struct word_info *info)
{
    size_t i;
    static GC bboxGC = 0;
    static Boolean highlight_inverted_bak;

    if (bboxGC != 0 && highlight_inverted_bak != resource.match_highlight_inverted) {
	XFreeGC(DISP, bboxGC);
	bboxGC = 0;
    }
    highlight_inverted_bak = resource.match_highlight_inverted;
    
    if (resource.match_highlight_inverted) {
	if (MAGNIFIER_ACTIVE && !INSIDE_MANE_WIN)
	    return;
	
	TRACE_FIND((stderr, "-- EXPOSED region: x %d, y %d, w %d, h %d",
		    globals.win_expose.min_x, globals.win_expose.min_y, globals.win_expose.max_x - globals.win_expose.min_x, globals.win_expose.max_y - globals.win_expose.min_y));
	
	if (bboxGC == 0) {
	    XGCValues values;
	    unsigned long valuemask;
	    
	    values.function = GXinvert; /* was: GXxor; see bug #850788 */
	    if (values.function == GXinvert) {
		valuemask = GCFunction;
	    }
	    else {
		values.foreground = WhitePixelOfScreen(SCRN) ^ BlackPixelOfScreen(SCRN);
		/* 		fprintf(stderr, "foreground: 0x%lx, white pixel: 0x%lx, black pixel: 0x%lx\n", */
		/* 			values.foreground, WhitePixelOfScreen(SCRN), BlackPixelOfScreen(SCRN)); */
		valuemask = GCFunction | GCForeground;
	    }
	    bboxGC = XCreateGC(DISP, XtWindow(globals.widgets.top_level), valuemask, &values);
	}
    }
    else if (bboxGC == 0) {
	XGCValues values;
	values.function = GXcopy;
	values.line_width = 2;
	values.cap_style = CapRound;
	values.foreground = resource.hl_Pixel;
	values.background = resource.back_Pixel;
	bboxGC = XCreateGC(DISP, XtWindow(globals.widgets.top_level),
			   GCFunction | GCLineWidth | GCCapStyle | GCForeground | GCBackground, &values);
    }

    TRACE_FIND((stderr, "bboxes_idx: %lu", (unsigned long)info->bboxes_idx));
    for (i = 0; info->bboxes != NULL && i <= info->bboxes_idx && info->bboxes[i].ulx < INT_MAX; i++) {
	int x, y, w, h;

	if (info->bboxes[i].ulx > 10000 || info->bboxes[i].uly > 10000) {
	    TRACE_FIND((stderr, "skipping box: x %d, y %d",
			info->bboxes[i].ulx, info->bboxes[i].uly));
 	    continue;
	}
	
	GET_BBOX(info->bboxes[i], currwin.shrinkfactor, x, y, w, h);
	TRACE_FIND((stderr, "DRAWING box: x %d, y %d, w %d, h %d; shrink: %d",
		    x, y, w, h, currwin.shrinkfactor));
	if (resource.match_highlight_inverted) {
	    if (clip_region(&x, &y, &w, &h)) {
		TRACE_FIND((stderr, "CLIPPED box: x %d, y %d, w %d, h %d",
			    x, y, w, h));
		TEST_DELAY("Redrawing BBOX ............... ");
		XFillRectangle(DISP, mane.win, bboxGC, x, y, w, h);
		TEST_DELAY("Inverting BBOX ............... ");
	    }
	}
	/* 	XDrawRectangle(DISP, mane.win, globals.gc.high, x, y, w + 0, h + 0); */
	/* 	clearexpose(&mane, x, y, w + 3, h + 3); */
	/* 	XDrawRectangle(DISP, mane.win, globals.gc.high, x, y, w + 2, h + 2); */
	/*         XDrawRectangle(DISP, mane.win, globals.gc.high, x + 1, y + 1, w, h); */
	else {
	    static XPoint points[5] = { {0,0}, {0,0}, {0,0}, {0,0}, {0,0} };
	    
	    h++;
	    
	    points[0].x = x;
	    points[0].y = y;
	    
	    points[1].x = w;
	    
	    points[2].y = h;
	    
	    points[3].x = -w;
	    
	    points[4].y = -h;
	    
	    XDrawLines(DISP, mane.win, bboxGC, points, 5, CoordModePrevious);
	}

	if (!MAGNIFIER_ACTIVE) {
	    /* note: inverted y_max y_min, since we want to display the full box here */
	    scroll_page_if_needed(x + w, x, y + h, y);
	}
    }
}

/* return true if we have a match on page pageno */
Boolean
search_have_match(int pageno)
{
    /*     fprintf(stderr, "pageno: %d --- m_info: %p\n", pageno, m_info); */
    /*     fprintf(stderr, "m_match_page: %d\n", m_match_page); */
    return m_info != NULL && m_match_page == pageno;
}

/* TODO: Speed this up, by doing something like bsearch? */
/*
  Returns 2 if we're at the 1st or last character of the bbox, 1 if
  inside the bbox, and 0 else (currently the return value 2 is unused).
*/
int
search_inside_bbox_match(int x, int y)
{
    size_t i;

    ASSERT(m_info != NULL, "inside_match musn't be called with m_info == NULL!");
    TRACE_FIND((stderr, "m_info->bboxes: %p; idx: %lu", (void *)m_info->bboxes, (unsigned long)m_info->bboxes_idx));
    for (i = 0; m_info->bboxes != NULL && i <= m_info->bboxes_idx && m_info->bboxes[i].ulx < INT_MAX; i++) {
	TRACE_FIND((stderr, "inside_bbox_match: %d, %d", x, y));
	TRACE_FIND((stderr, "x: %d, y: %d, x2: %d, y2: %d",
		    (int)(m_info->bboxes[i].ulx / (double)currwin.shrinkfactor + 0.5),
		    (int)(m_info->bboxes[i].uly / (double)currwin.shrinkfactor + 0.5),
		    (int)(m_info->bboxes[i].lrx / (double)currwin.shrinkfactor + 0.5),
		    (int)(m_info->bboxes[i].lry / (double)currwin.shrinkfactor + 0.5)));

	if (x >= (int)(m_info->bboxes[i].ulx / (double)currwin.shrinkfactor + 0.5)
	    && x <= (int)(m_info->bboxes[i].lrx / (double)currwin.shrinkfactor + 0.5)
	    && y >= (int)(m_info->bboxes[i].uly / (double)currwin.shrinkfactor + 0.5)
	    && y <= (int)(m_info->bboxes[i].lry / (double)currwin.shrinkfactor + 0.5)) {
	    TRACE_FIND((stderr, "%d == %d",
			x - 1, (int)(m_info->bboxes[i].ulx / (double)currwin.shrinkfactor + 0.5)));
	    TRACE_FIND((stderr, "%d == %d",
			y - 1, (int)(m_info->bboxes[i].uly / (double)currwin.shrinkfactor + 0.5)));
	    if (x - 1 == (int)(m_info->bboxes[i].ulx / (double)currwin.shrinkfactor + 0.5)
		&& y - 1 == (int)(m_info->bboxes[i].uly / (double)currwin.shrinkfactor + 0.5))
		return 2;
	    else
		return 1;
	}
    }
    return 0;
}

void
search_draw_inverted_regions(void)
{
    if (m_info != NULL) {
	draw_bboxes(m_info);
    }
}


/*
 * shift everything in info down by 1 pages, so that info for page 1 becomes
 * info for page 0.
 */
static void
shift_info_down(struct search_info *searchinfo, struct word_info *info, struct page_mapping *page_mapping)
{
    size_t initial_offset = 0;
    if (page_mapping[0].offset != -1) { /* remember value */
	initial_offset = page_mapping[0].offset;
    }
    
    if (globals.debug & DBG_FIND) {
	int i;
	fprintf(stderr, "current page_mapping:\n");
	for (i = 0; i < 2; i++) {
	    fprintf(stderr, "%d: %d\n", page_mapping[i].pageno, page_mapping[i].offset);
	}
    }
    
    TRACE_FIND((stderr, "initial offset: %lu", (unsigned long)initial_offset));

    page_mapping[0].offset = page_mapping[1].offset - initial_offset;
    page_mapping[0].pageno = page_mapping[1].pageno;
    ASSERT(page_mapping[0].offset >= 0, "new index mustn't be negative!\n");
    page_mapping[1].offset = page_mapping[1].pageno = -1;
    
    TRACE_FIND((stderr, "new val at 0: %d; curr_idx: %lu, len: %lu",
		page_mapping[0].offset,
		(unsigned long)(info->curr_buf_idx - initial_offset),
		(unsigned long)(strlen(info->txt_buf + initial_offset))));

    /* move the text buffer down, and update its current index accordingly */
    ASSERT(info->curr_buf_idx >= initial_offset, "info->curr_buf_idx mustn't become negative!");
    memmove(info->txt_buf, info->txt_buf + initial_offset, info->curr_buf_idx - initial_offset + 1);
    info->curr_buf_idx -= initial_offset;
    TRACE_FIND((stderr, "new index: %lu", (unsigned long)info->curr_buf_idx));

    searchinfo->from_pos -= initial_offset;
    searchinfo->to_pos -= initial_offset;
    if (searchinfo->from_pos < 0)
	searchinfo->from_pos = 0;
    if (searchinfo->to_pos < 0)
	searchinfo->to_pos = 0;
    TRACE_FIND((stderr, "new offsets: from=%d, to=%d", searchinfo->from_pos, searchinfo->to_pos));
}

/*
 * shift everything in info up by 1 page, so that info for page 0 becomes info
 * for page 1.
 */
static void
shift_info_up(struct word_info *info, struct page_mapping *page_mapping)
{
    if (globals.debug & DBG_FIND) {
	int i;
	fprintf(stderr, "current page_mapping:\n");
	for (i = 0; i < 2; i++) {
	    fprintf(stderr, "%d: %d\n", page_mapping[i].pageno, page_mapping[i].offset);
	}
    }
    
    ASSERT(page_mapping[0].offset > 0, "info->curr_buf_idx mustn't become negative!");
    info->curr_buf_idx = page_mapping[0].offset;
    TRACE_FIND((stderr, "new index: %lu", (unsigned long)info->curr_buf_idx));

    page_mapping[1].offset = page_mapping[0].offset;
    page_mapping[1].pageno = page_mapping[0].pageno;
    page_mapping[0].offset = page_mapping[0].pageno = -1;
    
}

static void
append_to_info(struct word_info *info, const char *str)
{
    size_t len = strlen(str);
    size_t new_size = info->txt_buf_size;
    while (len >= new_size) { /* space for trailing 0 */
	new_size++;
    }
    if (new_size > info->txt_buf_size) {
	info->txt_buf_size = new_size;
	info->txt_buf = xrealloc(info->txt_buf, sizeof *(info->txt_buf) * info->txt_buf_size);
    }
    memcpy(info->txt_buf + info->curr_buf_idx, str, len + 1); /* also copy trailing 0 */
    info->curr_buf_idx += len;
}

/* append info2 to info1, enlarging info1 as needed. */
static void
append_info(struct word_info *info1, const struct word_info *info2)
{
    if (info2->txt_buf_size > 0) {
	info1->txt_buf = xrealloc(info1->txt_buf, sizeof *(info1->txt_buf)
				  * (info1->txt_buf_size + info2->txt_buf_size));
	memcpy(info1->txt_buf + info1->curr_buf_idx, info2->txt_buf, info2->txt_buf_size);
	info1->txt_buf_size += info2->txt_buf_size;
	info1->curr_buf_idx += info2->curr_buf_idx;
    }
}

/* prepend info1 to info2, enlarging info2 as needed. */
static void
prepend_info(const struct word_info *info1, struct word_info *info2)
{
    if (info1->txt_buf_size > 0) {
	info2->txt_buf = xrealloc(info2->txt_buf, sizeof *(info2->txt_buf)
				  * (info2->txt_buf_size + info1->txt_buf_size + 1));
	memmove(info2->txt_buf + info1->curr_buf_idx, info2->txt_buf, info2->curr_buf_idx);
	info2->txt_buf[info1->curr_buf_idx + info2->curr_buf_idx] = '\0';
	memcpy(info2->txt_buf, info1->txt_buf, info1->curr_buf_idx);
	/* 	TRACE_FIND((stderr, "prepend_info: info2->txt_buf is: |%s|", info2->txt_buf)); */
	info2->txt_buf_size += info1->txt_buf_size;
	info2->curr_buf_idx += info1->curr_buf_idx;
    }
}

#if HAVE_REGEX_H
/* convert the encoding part (between the `.' and the `@') of the
   locale string passed as argument to UTF-8, and return the result
   in a newly allocated string which the caller is responsible for
   free()ing.
*/
static char *
locale_to_utf8(const char *locale)
{
    char *utf8_locale = xstrdup(locale);
    char *ptr;
    if ((ptr = strchr(utf8_locale, '.')) != NULL) {
	char *ptr2, *rest = "";
	if ((ptr2 = strchr(utf8_locale, '@')) != NULL)
	    rest = xstrdup(ptr2);

	utf8_locale = xrealloc(utf8_locale,
			       ptr - utf8_locale + strlen(".utf8") + strlen(rest) + 1);
	*ptr = '\0';
	utf8_locale = strcat(utf8_locale, ".utf8");
	if (ptr2 != NULL) {
	    utf8_locale = strcat(utf8_locale, rest);
	    free(rest);
	}
    }
    else {
	utf8_locale = xstrcat(utf8_locale, ".utf8");
    }
    return utf8_locale;
}
#endif /* HAVE_REGEX_H */

static Boolean
is_utf8_ideograph(const unsigned char *p)
{
    int len;
    uint32_t ucs4;
    
    if ((len = utf8_to_ucs4((const char *)p, &ucs4, strlen((const char*)p))) <= 0)
	return False;
    
    return is_ideograph(ucs4);
}


static void
dump_buffer(const struct word_info *info, size_t offset, FILE *fp, outputFormatT fmt)
{
    size_t i = offset, len, tot_len;

    if (info->txt_buf == NULL)
	return;
    
    tot_len = strlen(info->txt_buf);
    while (i < tot_len) {
	if (fmt == FMT_UTF8) { /* just dump as-is */
	    fputc(info->txt_buf[i++], fp);
	}
	else { /* convert to iso-latin1, rendering unknown characters as `?' */
	    uint32_t ucs4;
	    const char *ret;

	    /* first apply normalization heurisitcs also used by search */
	    len = utf8_to_ucs4(info->txt_buf + i, &ucs4, strlen(info->txt_buf + i));
	    if ((ret = search_normalize_chars(ucs4)) != NULL)
		fputs(ret, fp);
	    else if (ucs4 <= 0xff) /* in iso-latin1 range */
		fputc((unsigned char)ucs4, fp);
	    else
		fprintf(fp, "\\%.4lX", (unsigned long)ucs4);
	    i += len;
	}
    }
}

static void
scan_page(FILE *fp, int pageno, struct word_info *w_info)
{
    off_t pos_save;
    static ubyte my_scan_buffer[DVI_BUFFER_LEN];
    struct drawinf currinf_save;
    struct scan_info info;
    ubyte maxchar_save;

    reinit_text_scan(); /* to reset scanning heuristics (linebreaks etc.) */
    info.data = (void *)w_info;
    info.geom_special = NULL; /* no procedure here */
    
    /* Save file position */
    pos_save = save_file_status(globals.dvi_file.bak_fp, &currinf_save, &maxchar_save);
    
    lseek(fileno(fp), pageinfo_get_offset(pageno), SEEK_SET);
    memset((char *)&currinf.data, 0, sizeof currinf.data);
    currinf.tn_table_len = TNTABLELEN;
    currinf.tn_table = tn_table;
    currinf.tn_head = tn_head;

    /* point currinf to our own buffer: */
    G_dvi_buf_ptr = my_scan_buffer;
    
    currinf.pos = currinf.end = G_dvi_buf_ptr;
    currinf.virtual = NULL;
    
    geom_scan(text_do_char, fp, &info, pageno);

    /* Restore file status.  */
    restore_file_status(globals.dvi_file.bak_fp, currinf_save, maxchar_save, pos_save);
}


static Boolean
do_scan_page(struct word_info *w_info, struct search_settings *settings,
	     struct page_mapping *page_mapping, int buffer_offset, int pageno,
	     searchDirectionT direction)
{
    struct word_info page_info = { NULL, 0, 0, NULL, 0, 0, NULL, NULL, 0, False, True, False };
    page_info.settings = settings;
    ASSERT(buffer_offset >= 0, "buffer_offset must have been initialized");
    page_info.buffer_offset = buffer_offset;
    TRACE_FIND((stderr, "scanning page: %d; from_pos: %d", pageno, settings->searchinfo->from_pos));

    if (read_events(EV_NOWAIT) & EV_GE_FIND_CANCEL) {
	TRACE_FIND((stderr, "interrupted!!"));
	return False;
    }
    
    scan_page(globals.dvi_file.bak_fp, pageno, &page_info);

    /*     TRACE_FIND((stderr, "scanned buffer of length %d on page %d; contents: |%s|", */
    /* 		page_info.curr_buf_idx, pageno, page_info.txt_buf)); */
    
    /* terminate page */
    append_to_info(&page_info, "\n");
    if (direction == SEARCH_DOWN) {
	/* append to existing info */
	append_info(w_info, &page_info);
	page_mapping->offset = w_info->curr_buf_idx;
    }
    else {
	prepend_info(&page_info, w_info);
	/* 	TRACE_FIND((stderr, "buffer after prepending: |%s|", w_info->txt_buf)); */
	page_mapping->offset = page_info.curr_buf_idx;
    }
    page_mapping->pageno = pageno;
    
    return True;
}

#if HAVE_REGEX_H
static void
report_regexp_error(int errcode, regex_t *regex, const char *term, int flag)
{
    size_t n = regerror(errcode, regex, NULL, 0);
    char *err_buf = xmalloc(n);
    regerror(errcode, regex, err_buf, n);
    xdvi_bell();
    popup_message(XtNameToWidget(globals.widgets.top_level, "*find_popup"),
		  MSG_ERR,
		  NULL,
		  "Could not %s regular expression \"%s\": %s\n",
		  flag == 0 ? "compile" : "execute",
		  term, err_buf);
    free(err_buf);
}
#endif /* HAVE_REGEX_H */

static void
try_match(const struct word_info *info,
	  const struct search_settings *settings)
{
    char *res = NULL;
    size_t from_pos = 0;
    struct search_info *searchinfo = settings->searchinfo;
    
    ASSERT(info->curr_buf_idx == strlen(info->txt_buf), "");
    TRACE_FIND((stderr, "buffer index: %lu, len: %lu; from: %lu",
		(unsigned long)info->curr_buf_idx,
		(unsigned long)strlen(info->txt_buf),
		(unsigned long)searchinfo->from_pos));
    if (searchinfo->from_pos != -1)
	from_pos = searchinfo->from_pos;
    
    if (settings->direction == SEARCH_DOWN) {
	TRACE_FIND((stderr, "trying to match |%s| from %lu; total: %lu",
		    settings->utf8_term,
		    (unsigned long)from_pos,
		    (unsigned long)strlen(info->txt_buf)));

	res = strstr(info->txt_buf + from_pos, settings->utf8_term);
    }
    else {
	size_t curr_pos = 0;
	char *res_bak = NULL;

	TRACE_FIND((stderr, "UP; trying to match |%s| from %lu, %lu",
		    settings->utf8_term,
		    (unsigned long)curr_pos, (unsigned long)from_pos));
	TRACE_FIND((stderr, "buf: |%s|", info->txt_buf + curr_pos));
	while (curr_pos <= from_pos) {
	    res_bak = res;
	    res = strstr(info->txt_buf + curr_pos, settings->utf8_term);
	    if (res != NULL) {
		curr_pos = res - info->txt_buf + strlen(settings->utf8_term);
	    }
	    else {
		break;
	    }
	}
	res = res_bak;
    }
    if (res != NULL) {
	searchinfo->have_match = True;
	searchinfo->from_pos = res - info->txt_buf;
	searchinfo->to_pos = searchinfo->from_pos + strlen(settings->utf8_term);
    }
    else {
	searchinfo->have_match = False;
    }
}

#if HAVE_REGEX_H
static Boolean
try_regexp_match(regex_t *regex,
		 const struct word_info *info,
		 const struct search_settings *settings)
{
    int have_match;
    regmatch_t re_match;
    size_t from_pos = 0;
    Boolean retval = True;

    struct search_info *searchinfo = settings->searchinfo;
    
#if SWITCH_TO_UTF8
    /* switch to UTF-8 encoding, as for regcomp() */
    char *utf8_locale = locale_to_utf8(globals.orig_locale);
    TRACE_FIND((stderr, "current locale: |%s|, utf8 version: |%s|", globals.orig_locale, utf8_locale));
    setlocale(LC_ALL, utf8_locale);
#endif

    re_match.rm_so = re_match.rm_eo = -1;
    
    if (searchinfo->from_pos != -1)
	from_pos = searchinfo->from_pos;
    
    if (settings->direction == SEARCH_DOWN) {
	Boolean search_again = True;
	
	while (search_again && from_pos < info->curr_buf_idx) {
	    searchinfo->have_match = False;
	    search_again = False;

	    /* 	    TRACE_FIND((stderr, "search string: |%s|, from_pos: %d", info->txt_buf + from_pos, from_pos)); */
	    have_match = regexec(regex, info->txt_buf + from_pos, 1, &re_match, 0);

	    if (have_match == 0) { /* match */
		TRACE_FIND((stderr, "match from %d to %d", (int)re_match.rm_so, (int)re_match.rm_eo));
		if (re_match.rm_so == re_match.rm_eo) { /* skip zero-length matches */
		    from_pos += re_match.rm_so + 1;
		    search_again = True;
		}
		else {
		    searchinfo->from_pos = re_match.rm_so + from_pos;
		    searchinfo->to_pos = re_match.rm_eo + from_pos;
		    searchinfo->have_match = True;
		}
	    }
	    else if (have_match != REG_NOMATCH) { /* error */
		report_regexp_error(have_match, regex, settings->term, 1);
		retval = False;
		break;
	    }
	}
    }
    else { /* searching backwards */
	size_t curr_pos = 0, prev_pos = 0;
	regmatch_t re_match_bak;
	Boolean search_again = True;
	searchinfo->have_match = False;
	TRACE_FIND((stderr, "UP; trying to match |%s| from %lu, %lu",
		    settings->utf8_term,
		    (unsigned long)curr_pos, (unsigned long)from_pos));

	re_match_bak.rm_so = re_match_bak.rm_eo = -1;
	
	while (search_again && curr_pos <= from_pos) {
	    search_again = False;

	    /* remember previous match data */
	    re_match_bak.rm_so = prev_pos + re_match.rm_so;
	    re_match_bak.rm_eo = prev_pos + re_match.rm_eo;

	    TRACE_FIND((stderr, "backup values: %d, %d; curr_pos: %lu",
			(int)re_match_bak.rm_so, (int)re_match_bak.rm_eo, (unsigned long)curr_pos));

	    /* 	    TRACE_FIND((stderr, "text buf: |%s|", info->txt_buf + curr_pos)); */
	    have_match = regexec(regex, info->txt_buf + curr_pos, 1, &re_match, 0);
	    while (have_match == 0 && re_match.rm_so == re_match.rm_eo) { /* skip zero-length matches */
		have_match = regexec(regex, info->txt_buf + ++curr_pos, 1, &re_match, 0);
	    }

	    
	    if (have_match == 0) { /* match */
		TRACE_FIND((stderr, "match from %d to %d", (int)re_match.rm_so, (int)re_match.rm_eo));
		TRACE_FIND((stderr, "updating pos: %lu -> %lu; from_pos: %lu",
			    (unsigned long)curr_pos,
			    (unsigned long)(curr_pos + re_match.rm_eo),
			    (unsigned long)from_pos));
		prev_pos = curr_pos;
		curr_pos += re_match.rm_eo;
		search_again = True;
	    }
	    else if (have_match != REG_NOMATCH) { /* error */
		report_regexp_error(have_match, regex, settings->term, 1);
		retval = False;
		break;
	    }
	}
	if (retval) {
	    re_match.rm_so = re_match_bak.rm_so;
	    re_match.rm_eo = re_match_bak.rm_eo;
	    
	    if (re_match.rm_so != -1) {
		TRACE_FIND((stderr, "remembering from-to: %d - %d", (int)re_match.rm_so, (int)re_match.rm_eo));
		searchinfo->from_pos = re_match.rm_so;
		searchinfo->to_pos = re_match.rm_eo;
		searchinfo->have_match = True;
	    }
	}
    }

#if SWITCH_TO_UTF8
    /* switch back to original encoding */
    setlocale(LC_ALL, globals.orig_locale);
    setlocale(LC_NUMERIC, "C");
    free(utf8_locale);
#endif
    
    return retval;
}
#endif /* HAVE_REGEX_H */

/* Erase exising highlighting. TODO: Move this into dvi-draw so that
   there's no big delay between erasing and drawing the next marker.
*/
static void
erase_match_highlighting(struct word_info *info, Boolean flag)
{
    size_t i;

    if (info == NULL || (MAGNIFIER_ACTIVE && !INSIDE_MANE_WIN))
	return;

    for (i = 0; info->bboxes != NULL && i <= info->bboxes_idx && info->bboxes[i].ulx < INT_MAX; i++) {
	int x, y, w, h;
	GET_BBOX(info->bboxes[i], currwin.shrinkfactor, x, y, w, h);
	if (resource.match_highlight_inverted) {
	    if (flag || m_highlight_region_changed) {
		clearexpose(&mane, x, y, w, h);
	    }
	    else if (clip_region(&x, &y, &w, &h)) {
		TRACE_FIND((stderr, "ERASING box: x %d, y %d, w %d, h %d",
			    x, y, w, h));
		TEST_DELAY("Showing BBOX ............... ");
		XClearArea(DISP, mane.win, x, y, w, h, False);
		TEST_DELAY("Clearing BBOX ............... ");
	    }
	}
	else if (flag) {
	    /* erase only the existing marks, not the entire bounding box, to avoid flashing of
	       the text inside the bounding box. 1 pixel added to w, h just to make sure in case of
	       rounding errors. */
	    h++;
	    clearexpose(&mane, x - 1, y - 1, 2, h + 2);
	    clearexpose(&mane, x - 1, y - 1, w + 2, 2);
	    clearexpose(&mane, x + w - 1, y - 1, 2, h + 2);
	    clearexpose(&mane, x - 1, y + h - 1, w + 2, 2);
	}
    }
    if (flag) {
	reset_info(info);
	info = NULL;
    }
    else
	m_highlight_region_changed = False;
}

static void
highlight_match(struct search_settings *settings,
		const struct word_info *w_info,
		const struct page_mapping *page_mapping)
{
    const int context = 10;
    int match_page, i, j = 0;
    static struct word_info curr_info = { NULL, 0, 0, NULL, 0, 0, NULL, NULL, 0, True, False, False };
    struct search_info *searchinfo = settings->searchinfo;
    Boolean match_wrapped = False;
    
    curr_info.settings = settings;
    curr_info.page_mapping = page_mapping;

    TRACE_FIND((stderr, "MATCH from pos %d to %d:",
		searchinfo->from_pos, searchinfo->to_pos));
    
    if (globals.debug & DBG_FIND) {
	fprintf(stderr, "current page_mapping:\n");
	for (i = 0; i < 2; i++) {
	    fprintf(stderr, "%d: %d\n", page_mapping[i].pageno, page_mapping[i].offset);
	}
    }

    TRACE_FIND((stderr, "to_pos: %d, %d", searchinfo->to_pos, page_mapping[0].offset));
    if (searchinfo->from_pos < page_mapping[0].offset) {
	match_page = page_mapping[0].pageno;
	if (searchinfo->to_pos > page_mapping[0].offset) {
	    match_wrapped = True;
	}
    }
    else {
	if (searchinfo->from_pos >= page_mapping[1].offset) {
	    XDVI_ERROR((stderr, "to_pos (%d) should be smaller than page_mapping[1].offset (%d)!",
			searchinfo->from_pos, page_mapping[1].offset));
	}
	ASSERT(searchinfo->from_pos < page_mapping[1].offset, "to_pos should be smaller than page_mapping[1].offset!");
	match_page = page_mapping[1].pageno;
	if (searchinfo->to_pos > page_mapping[1].offset) {
	    match_wrapped = True;
	}
    }
    if (!settings->isearchterm) {
	if (match_wrapped)
	    statusline_info(STATUS_MEDIUM, "Match from page %d to %d", match_page + 1,  match_page + 2);
	else
	    statusline_info(STATUS_MEDIUM, "Match on page %d", match_page + 1);
    
	if (settings->wrapcnt > 0)
	    statusline_append(STATUS_MEDIUM, " (wrapped)", " (wrapped)");
    }
    if (globals.debug & DBG_FIND) {
	fprintf(stderr, "*** match_page: %d, adding: %d\n", match_page, searchinfo->page_offset);
    
	for (j = searchinfo->from_pos - context; j < searchinfo->from_pos; j++) {
	    if (j >= 0)
		fputc(w_info->txt_buf[j], stderr);
	    else
		fputc(' ', stderr);
	}
	fputs("\n        >>", stderr);
	for (j = searchinfo->from_pos; j < searchinfo->to_pos; j++) {
	    fputc(w_info->txt_buf[j], stderr);
	}
	fputs("<<\n          ", stderr);
	for (j = searchinfo->from_pos; j < searchinfo->to_pos; j++) {
	    fputc(' ', stderr);
	}
	for (j = searchinfo->to_pos; j < (int)w_info->curr_buf_idx; j++) {
	    fputc(w_info->txt_buf[j], stderr);
	    if (w_info->txt_buf[j] == '\n')
		break;
	}
	fputc('\n', stderr);
    }
    
    if (match_page != current_page) {
	goto_page(match_page, resource.keep_flag ? NULL : home, False);
	page_history_insert(match_page);
	/* 	globals.ev.flags |= EV_NEWPAGE; */
    }
    /* don't raise the window - this can obscure the search popup */
    /*     XMapRaised(DISP, XtWindow(globals.widgets.top_level)); */

    /* this erases contents of m_info, so be careful - contents of curr_info
       will be indirectly erased by this ... */
    erase_match_highlighting(m_info, True);
    
    /* now rescan the page to get bounding box info for the match */
    scan_page(globals.dvi_file.bak_fp, match_page, &curr_info);
    m_info = &curr_info;
    
    m_match_page = match_page;
    
    do_autoscroll = True; /* enable scrolling to match */
    /* create an expose event so that the region gets highlighted */
    if (m_info != NULL) {
	generate_highlight_expose(m_info);
    }
}

#if HAVE_REGEX_H

/*
 * A mapping of Perl-style character classes to POSIX-style character classes.
 */
static struct perl2posix_mapping {
    const char *perl_class;
    const char *posix_class;
} perl2posix_map[] = {
    { "\\w", "[[:alnum:]]" },
    { "\\W", "[^[:alnum:]]" },
    { "\\d", "[[:digit:]]" },
    { "\\D", "[^[:digit:]]" },
    { "\\s", "[[:space:]]" },
    { "\\S", "[^[:space:]]" },
    { NULL, NULL } /* terminate */
};

/*
 * Convert `perl_regex', a regexp using Perl-style character classes,
 * to a regexp using POSIX-style character classes. Return the latter
 * in freshly allocated memory, which the caller is responsible to free().
 * Uses the perl2posix_map table.
 */
static char *
perl2posix(const char *perl_regex)
{
    char *posix_regex = xstrdup(perl_regex);
    size_t i;

    for (i = 0; perl2posix_map[i].perl_class != NULL; i++) {
	const char *ptr;
	size_t offset = 0;
	TRACE_FIND((stderr, "searching for |%s| at offset %lu", perl2posix_map[i].perl_class, (unsigned long)offset));
	while ((ptr = find_format_str(posix_regex + offset, perl2posix_map[i].perl_class)) != NULL) {
	    size_t len1 = ptr - posix_regex; /* length up to match */
	    size_t len2 = strlen(perl2posix_map[i].posix_class);
	    TRACE_FIND((stderr, "Regexp: mapping %s to %s",
			perl2posix_map[i].perl_class,
			perl2posix_map[i].posix_class));
	    /* -1 since -2 for perl format string, +1 for trailing '\0' */
	    posix_regex = xrealloc(posix_regex, strlen(posix_regex) + len2 - 1);
	    /* make space for new format string */
	    memmove(posix_regex + len1 + len2, posix_regex + len1 + 2, strlen(posix_regex + len1 + 2) + 1);
	    /* copy in new format string */
	    memcpy(posix_regex + len1, perl2posix_map[i].posix_class, len2);
	    TRACE_FIND((stderr, "Expanded regex: |%s|", posix_regex));
	    offset = len1 + len2;
	    TRACE_FIND((stderr, "searching again for |%s| at offset %lu",
			perl2posix_map[i].perl_class,
			(unsigned long)offset));
	}
    }
    return posix_regex;
}

#endif /* HAVE_REGEX_H */

char *
get_text_selection(int *len, int ulx, int uly, int lrx, int lry)
{
    struct word_info txt_info = { NULL, 0, 0, NULL, 0, 0, NULL, NULL, 0, False, False, True };
    struct bbox text_bbox;
    
    text_bbox.ulx = ulx;
    text_bbox.uly = uly;
    text_bbox.lrx = lrx;
    text_bbox.lry = lry;

    txt_info.bboxes = &text_bbox;

    /* initialize buffer with empty string */
    txt_info.txt_buf_size = 1;
    txt_info.txt_buf = xmalloc(txt_info.txt_buf_size);
    txt_info.txt_buf[0] = '\0';

    /* this enlarges the buffer as needed */
    scan_page(globals.dvi_file.bak_fp, current_page, &txt_info);
    
    *len = txt_info.txt_buf_size;
    return txt_info.txt_buf;
    
#if 0
    /*     fprintf(stderr, "========== SELECTION (len %d):\n", txt_info.curr_buf_idx); */
    /*     dump_buffer(&txt_info, 0, stderr, FMT_ISO_8859_1); */
    /*     fprintf(stderr, "==========\n"); */

    buf = xmalloc(4 * txt_info.curr_buf_idx + 1); /* just in case we get many non-printables */
    while (i < txt_info.curr_buf_idx) {
	uint32_t ucs4;
	const char *ret;
	
	/* first apply normalization heurisitcs also used by search */
	size_t len = utf8_to_ucs4(txt_info.txt_buf + i, &ucs4, strlen(txt_info.txt_buf + i));
	if ((ret = search_normalize_chars(ucs4)) != NULL) {
	    size_t len_ret = strlen(ret);
	    memcpy(buf + offset, ret, len_ret);
	    offset += len_ret;
	}	
	else if (ucs4 <= 0xff) { /* in iso-latin1 range */
	    buf[offset++] = (unsigned char)ucs4;
	}
	else {
	    sprintf(buf + offset, "\\%.4lX", ucs4);
	    offset += 4;
	}
	i += len;
    }
    buf[offset] = '\0';
    free(txt_info.txt_buf);
    return buf;
#endif /* 0 */
}

Boolean
search_extract_text(struct save_or_print_info *info)
{
    int i;
    FILE *fp;
    
    if ((fp = XFOPEN(info->finfo->out_file, "wb")) == NULL) {
	popup_message(globals.widgets.top_level,
		      MSG_ERR,
		      NULL, "Could not open %s for writing: %s.",
		      info->finfo->out_file,
		      strerror(errno));
	return False;
    }

    for (i = 0; i < total_pages; i++) {
	if (info->pinfo->callback == NULL || info->pinfo->callback(info, i)) {
	    struct word_info txt_info = { NULL, 0, 0, NULL, 0, 0, NULL, NULL, 0, False, False, False };
	    scan_page(globals.dvi_file.bak_fp, i, &txt_info);

	    dump_buffer(&txt_info, 0, fp, info->fmt);

	    free(txt_info.txt_buf);
	    
	    fputs("\n\n", fp); /* two newlines for page breaks */
	}
    }
    
    fclose(fp);
    return True;
}

void
search_erase_highlighting(Boolean reset)
{
    erase_match_highlighting(m_info, reset);
}

static void
message_search_ended(XtPointer arg)
{
    struct search_settings *settings = (struct search_settings *)arg;

    settings->searchinfo->from_pos = settings->searchinfo->to_pos = -1;
    TRACE_FIND((stderr, "search ended; current_page: %d", current_page));
    settings->from_page = current_page;
    search_signal_page_changed();
    settings->message_window = 0;
    /*     statusline_append(STATUS_SHORT, " stopped."); */
    if (!settings->isearchterm)
	statusline_info(STATUS_SHORT, "Search stopped.");
}


void
search_restart(XtPointer arg)
{
    struct search_settings *settings = (struct search_settings *)arg;
    Widget popup, textfield;
    char *searchterm = NULL;
    char *textfield_name = NULL;
    
    TRACE_FIND((stderr, "restart search!"));

    /* Update the search term (user might have changed it before
       hitting `Return' to restart the search).  We need to start
       from the toplevel widget since this method may be called
       from a different window (e.g. confirmation popup).
    */
#if defined(MOTIF) && USE_COMBOBOX
    textfield_name = "Text";
#else
    textfield_name = Xdvi_SEARCHBOX_INPUT_NAME;
#endif /* defined(MOTIF) && USE_COMBOBOX */
    if (!settings->isearchterm
	&& get_widget_by_name(&popup, globals.widgets.top_level, Xdvi_SEARCH_POPUP_NAME, True)
	&& get_widget_by_name(&textfield, popup, textfield_name, True)) {
	XtVaGetValues(textfield,
#ifdef MOTIF
		      XmNvalue,
#else
		      XtNstring,
#endif
		      &searchterm, NULL);
	if (searchterm == NULL) {
	    XDVI_WARNING((stderr, "Got NULL searchterm in search_restart()!"));
	    return;
	}
	settings->term = searchterm;
    }

    if (settings->direction == SEARCH_DOWN) {
	settings->from_page = 0;
    }
    else {
	settings->from_page = total_pages - 1;
    }
    if (settings->direction == SEARCH_DOWN)
	settings->searchinfo->from_pos = settings->searchinfo->to_pos = -1;
    else
	settings->searchinfo->from_pos = settings->searchinfo->to_pos = INT_MAX;
    
    settings->message_window = 0;
    search_signal_page_changed();
    search_dvi((XtPointer)settings);
}

static void
normalize_newline(char *str)
{
    size_t i, j;
    for (i = 0, j = 0; str[i] != '\0'; i++, j++) {
	if (str[i] == '\\' && str[i + 1] == 'n') {
	    if (i > 0 && str[i - 1] == '\\') {
		str[j] = 'n';
		i++;
	    }
	    else {
		str[j] = '\n';
		i++;
	    }
	}
	else {
	    str[j] = str[i];
	}
    }
    str[j] = str[i]; /* copy terminating '\0' */
}


static Boolean
reinit_searchterm(struct search_settings *settings, const char *encoding)
{
    struct search_info *searchinfo = settings->searchinfo;

    free(settings->utf8_term);
    settings->utf8_term = NULL;

    if (memicmp(encoding, "iso-8859-1", strlen("iso-8859-1")) == 0
	|| memicmp(encoding, "iso8859-1", strlen("iso8859-1")) == 0) {
	int conv_len = (strlen(settings->term) + 1) * 2;
	int ret_len;
	char *ptr = xmalloc(conv_len);
	if ((ret_len = str_iso_8859_1_to_utf8(settings->term, ptr, conv_len)) < 0) {
	    xdvi_bell();
	    popup_message(XtNameToWidget(globals.widgets.top_level, "*find_popup"),
			  MSG_ERR,
			  NULL,
			  "Shouldn't happen: search term `%s' is too long (> %d)", settings->term, conv_len);
	    searchinfo->locked = False;
	    free(ptr);
	    settings->utf8_term = xstrdup("");
	    return False;
	}
	settings->utf8_term = ptr;
    }
    else if (memicmp(encoding, "utf-8", strlen("utf-8")) == 0
	     || memicmp(encoding, "utf8", strlen("utf8")) == 0) {
	settings->utf8_term = xstrdup(settings->term);
    }
    else {
	if ((settings->utf8_term = iconv_convert_string(encoding, "utf-8", settings->term)) == NULL) {
	    return False;
	}
    }

    TRACE_FIND((stderr, "UTF-8 search term: |%s|", settings->utf8_term));
    normalize_newline(settings->utf8_term);
    TRACE_FIND((stderr, "UTF-8 search term after normalizing newline: |%s|", settings->utf8_term));

#if 0
    /* lowercasing for regexps is dealt with by REG_ICASE */
    if (!settings->case_sensitive && !settings->use_regexp) {
        if (!utf8_lowercase(settings->utf8_term))
            return False;
        TRACE_FIND((stderr, "Lowercased UTF-8 search term: |%s|", settings->utf8_term));
    }
#else /* always lowercase, since REG_ICASE is broken with UTF-8(?) */
    if (!settings->case_sensitive && !utf8_lowercase(settings->utf8_term))
	return False;
    TRACE_FIND((stderr, "Lowercased UTF-8 search term: |%s|", settings->utf8_term));
#endif /* 1 */
    
    /* remove spaces/newlines before/after an ideographic char */
    {
	unsigned char *p = (unsigned char *)settings->utf8_term;
	unsigned char *q;
	int l, len = strlen((char *)p);
	uint32_t ucs4;
	Boolean had_ideograph;
	
	had_ideograph = False;
	while (len > 0) {
	    if (*p == ' ' || *p == '\n') {
		q = p + 1;
		while (*q == ' ' || *q == '\n')
		    q++;
		len -= q - p;
		if (had_ideograph || is_utf8_ideograph(q))
		    memmove(p, q, len + 1);	/* remove spaces/newlines */
		else
		    p = q;			/* preserve spaces/newlines */
	    }
	    else {
		if ((l = utf8_to_ucs4((char *)p, &ucs4, len)) <= 0)
		    break;
		len -= l;
		had_ideograph = is_ideograph(ucs4);
		p += l;
	    }
	}

	settings->utf8_term = xrealloc(settings->utf8_term, strlen(settings->utf8_term) + 1);
    }
    
    return True;
}

void
search_reset_info(void)
{
    TRACE_FIND((stderr, "resetting info!"));
    reset_info(m_info);
    m_info = NULL;
}


#if HAVE_REGEX_H
static Boolean
do_recompile_regexp(struct search_settings *settings,
		    regex_t *regex)
{
    int re_retval;
    /* use extended POSIX, and make `.' not match newlines (otherwise it's
       often too unintuitive because of the greediness of '+'/'*' */
    int re_flags = REG_EXTENDED | REG_NEWLINE;
    TRACE_FIND((stderr, "compiling regexp ..."));
    
    if (settings->posix_term != NULL) {
	TRACE_FIND((stderr, "freeing old regexp ..."));
	free(settings->posix_term);
	regfree(regex);
    }
    settings->posix_term = perl2posix(settings->utf8_term);

#if 0 /* this is broken with e.g. german umlauts; maybe it can't deal with UTF-8 encoding? */
    /* compile regexp from search term */
    if (!settings->case_sensitive)
        re_flags |= REG_ICASE;
#endif
    
    { /* change the encoding part of the locale to UTF-8 to match encoding
	 of the search string and the search buffer */
#if SWITCH_TO_UTF8
	char *utf8_locale = locale_to_utf8(globals.orig_locale);
	TRACE_FIND((stderr, "current locale: |%s|, utf8 version: |%s|", globals.orig_locale, utf8_locale));
        setlocale(LC_ALL, utf8_locale);
#endif
        re_retval = regcomp(regex, settings->posix_term, re_flags);
#if SWITCH_TO_UTF8
        setlocale(LC_ALL, globals.orig_locale);
        setlocale(LC_NUMERIC, "C");
	free(utf8_locale);
#endif
    }

    if (re_retval != 0) {
	report_regexp_error(re_retval, regex, settings->term, 0);
	settings->searchinfo->locked = False;
	return False;
    }

    return True;
}

#else  /* HAVE_REGEX_H */

static void
warn_no_regex(void)
{
    xdvi_bell();
    popup_message(XtNameToWidget(globals.widgets.top_level, "*find_popup"),
		  MSG_WARN,
		  NULL,
		  "POSIX regular expression support (regex.h) is not available on this platform; "
		  "using ordinary string matching instead.");
}
#endif /* HAVE_REGEX_H */

/*
  Scan at most two adjacent pages, writing results in to page_mapping.
  Return False if user cancelled the search, True else.
*/
static Boolean
scan_two_pages(struct search_settings *settings,
	       struct word_info *info,
	       struct page_mapping *page_mapping,
	       int curr_page)
{
    Boolean cancelled = False;
    struct search_info *searchinfo = settings->searchinfo;
    
    if (curr_page != page_mapping[0].pageno
	&& curr_page != page_mapping[1].pageno) { /* none of 2 pages scanned yet */
	reset_info(info);
	if (settings->direction == SEARCH_DOWN) {
	    if (!do_scan_page(info, settings, &(page_mapping[0]),
			      0, curr_page, settings->direction)
		|| (curr_page + 1 < total_pages &&
		    !do_scan_page(info, settings, &(page_mapping[1]),
				  page_mapping[0].offset, curr_page + 1, settings->direction)))
		cancelled = True;
	}
	else {
	    if (!do_scan_page(info, settings, &(page_mapping[1]),
			      0, curr_page, settings->direction)
		|| (curr_page > 0 &&
		    !do_scan_page(info, settings, &(page_mapping[0]),
				  page_mapping[1].offset, curr_page - 1, settings->direction)))
		cancelled = True;
	    else if (page_mapping[0].offset != -1) {
		page_mapping[1].offset += page_mapping[0].offset;
		if (searchinfo->from_pos != INT_MAX)
		    searchinfo->from_pos += page_mapping[0].offset;
	    }
	}
    }
    else if (curr_page != page_mapping[0].pageno) { /* current page scanned as page_mapping[1] */
	if (settings->direction == SEARCH_DOWN && curr_page + 1 < total_pages) {
	    shift_info_down(searchinfo, info, page_mapping);
	    if (!do_scan_page(info, settings, &(page_mapping[1]),
			      page_mapping[0].offset, curr_page + 1, settings->direction))
		cancelled = True;
	}
	else if (curr_page - 1 != page_mapping[0].pageno && curr_page > 0) {
	    if (!do_scan_page(info, settings, &(page_mapping[0]),
			      0, curr_page - 1, settings->direction))
		cancelled = True;
	    else {
		page_mapping[1].offset += page_mapping[0].offset;
		if (searchinfo->from_pos != INT_MAX)
		    searchinfo->from_pos += page_mapping[0].offset;
	    }
	}
    }
    else if (curr_page != page_mapping[1].pageno) { /* current page scanned as page_mapping[0] */
	if (settings->direction == SEARCH_UP && curr_page > 0) {
	    shift_info_up(info, page_mapping);
	    if (!do_scan_page(info, settings, &(page_mapping[0]),
			      0, curr_page - 1, settings->direction))
		cancelled = True;
	    else {
		page_mapping[1].offset += page_mapping[0].offset;
		if (searchinfo->from_pos != INT_MAX) {
		    searchinfo->from_pos += page_mapping[0].offset;
		    searchinfo->to_pos += page_mapping[0].offset;
		}
		TRACE_FIND((stderr, "new offsets: from=%d, to=%d", searchinfo->from_pos, searchinfo->to_pos));
	    }
	}
	else if (curr_page + 1 != page_mapping[1].pageno && curr_page + 1 < total_pages) {
	    if (!do_scan_page(info, settings, &(page_mapping[1]),
			      page_mapping[0].offset, curr_page + 1, settings->direction))
		cancelled = True;
	}
    }
    
    return !cancelled;
}

static void
search_over_pages(struct search_settings *settings,
		  struct page_mapping *page_mapping,
		  struct word_info *w_info,
		  int *from_pos_bak,
		  Boolean adjust_hyphen_offset
#if HAVE_REGEX_H
		  , regex_t *regex
#endif
		  )
{
    int curr_page = settings->from_page; /* page on which we're currently on */
    
    for (;;) {	/* scan pages, try to match */
	if (settings->isearchterm) {
	    const char *prefix = "";
	    if (settings->wrapcnt > 0)
		prefix = "Wrapped ";
	    statusline_info(STATUS_FOREVER, "%sI-search (ESC to exit): %s", prefix, settings->isearchterm);
	}
	else {
	    if (resource.expert_mode & XPRT_SHOW_STATUSLINE) /* too distracting for stdout */
		statusline_info(STATUS_MEDIUM, "Searching on page %d", curr_page);
	}
	TRACE_FIND((stderr, "curr_page: %d, pageno 0: %d, pageno1: %d",
		    curr_page, page_mapping[0].pageno, page_mapping[1].pageno));

	settings->hyphen_delta = 0;
	/* scan_two_pages will return False if user cancelled */
	if (!scan_two_pages(settings, w_info, page_mapping, curr_page)
	    || (read_events(EV_NOWAIT) & EV_GE_FIND_CANCEL)) {
	    if (!settings->isearchterm) {
		if (resource.expert_mode & XPRT_SHOW_STATUSLINE) /* too distracting for stdout */
		    statusline_append(STATUS_SHORT,
				      "- cancelled.",
				      "- cancelled.");
	    }
	    settings->searchinfo->locked = False;
	    return;
	
	}

	TRACE_FIND((stderr, "page mapping:\n%d: %d\n%d: %d",
		    page_mapping[0].pageno, page_mapping[0].offset,
		    page_mapping[1].pageno, page_mapping[1].offset));
	
	/*  	dump_buffer(w_info, 0, stderr, FMT_ISO_8859_1); */

	/* If ignore_hyphens has changed (in which case adjust_hyphen_offset
	   is true), the buffer will contain settings->hyphen_delta fewer
	   or more characters than in the previous pass due to the removal
	   or addition of hyphens; adjust from_pos and to_pos accordingly:
	*/
	if (adjust_hyphen_offset) {
	    TRACE_FIND((stderr, "adjusting offset by %d", settings->hyphen_delta));
	    if (settings->ignore_hyphens) { /* fewer characters */
		settings->searchinfo->from_pos -= settings->hyphen_delta;
		settings->searchinfo->to_pos -= settings->hyphen_delta;
	    }
	    else { /* more characters */
		settings->searchinfo->from_pos += settings->hyphen_delta;
		settings->searchinfo->to_pos += settings->hyphen_delta;
	    }
	    TRACE_FIND((stderr, "NEW from_pos: %d; to_pos: %d",
			settings->searchinfo->from_pos, settings->searchinfo->to_pos));
	}

    	
	/* match the searchstring */
#if HAVE_REGEX_H
	if (settings->use_regexp) {
	    if (!try_regexp_match(regex, w_info, settings)) /* regexp error */
		return;
	    
	}
	else {
#endif
	    try_match(w_info, settings);
#if HAVE_REGEX_H
	}
#endif

	/* again, check if user cancelled */
	if (read_events(EV_NOWAIT) & EV_GE_FIND_CANCEL) { /* user cancelled */
	    if (!settings->isearchterm)
		statusline_append(STATUS_SHORT,
				  "- cancelled.",
				  "- cancelled.");
	    settings->searchinfo->locked = False;
	    return;
	
	}
	
	if (settings->searchinfo->have_match) { /* match, highlight it */
	    highlight_match(settings, w_info, page_mapping);
	    settings->searchinfo->locked = False;
	    if (settings->direction == SEARCH_DOWN && !settings->isearchterm) {
		*from_pos_bak = settings->searchinfo->from_pos;
		settings->searchinfo->from_pos = settings->searchinfo->to_pos;
	    }
	    break;
	}
	else if ((settings->direction == SEARCH_DOWN && curr_page + 1 < total_pages)
		 || (settings->direction == SEARCH_UP && curr_page > 0)) {
	    if (settings->direction == SEARCH_DOWN)
		curr_page++;
	    else
		curr_page--;
	    TRACE_FIND((stderr, "continuing on page %d", curr_page));
	    erase_match_highlighting(m_info, True);
	    /* no match, and we have more pages; continue scanning */
	    continue;
	}
	else { /* reached end of file */
	    Widget find_popup;

	    if (settings->isearchterm) {
		const char *prefix = "Failed";
		fprintf(stderr, "reached end with wrapcnt: %d\n", settings->wrapcnt);
		if (settings->wrapcnt > 0)
		    prefix = "Failed wrapped";
		xdvi_bell();
		statusline_info(STATUS_FOREVER, "%s I-search (ESC to exit, RET to restart): %s", prefix, settings->term);
		settings->searchinfo->locked = False;
		erase_match_highlighting(m_info, True);
		settings->wrapcnt++;
		return;
	    }
	    
	    if (settings->wrap) {
		if (settings->wrapcnt > 0) {
		    positioned_popup_message(XtNameToWidget(globals.widgets.top_level, "*find_popup"),
					     MSG_INFO,
					     settings->x_pos, settings->y_pos,
					     NULL, "Pattern `%s' not found.", settings->term);
		    /* 		    positioned_choice_dialog(XtNameToWidget(globals.widgets.top_level, "*find_popup"), */
		    /* 					     MSG_INFO, */
		    /* 					     settings->x_pos, settings->y_pos, */
		    /* 					     NULL, */
		    /* #ifndef MOTIF */
		    /* 					     NULL, */
		    /* #endif */
		    /* 					     NULL, NULL, /\* pre callback *\/ */
		    /* 					     "OK", message_search_ended, (XtPointer)settings, */
		    /* 					     NULL, NULL, NULL, */
		    /* 					     "Pattern `%s' not found.", settings->term); */
		    settings->searchinfo->locked = False;
		    message_search_ended((XtPointer)settings);
		    return;
		}
		settings->wrapcnt++;
		erase_match_highlighting(m_info, True);
		settings->searchinfo->locked = False;
		search_restart((XtPointer)settings);
		return;
	    }
	    
	    if ((find_popup = XtNameToWidget(globals.widgets.top_level, "*find_popup")) == 0) {
		XDVI_WARNING((stderr, "Couldn't find \"find_popup\" widget!"));
		find_popup = globals.widgets.top_level;
	    }

	    if (!settings->isearchterm)
		statusline_append(STATUS_MEDIUM,
				  "... searched to end of file.",
				  "... searched to end of file.");
	    erase_match_highlighting(m_info, True);
	    settings->searchinfo->locked = False;
	    
	    settings->message_window =
		positioned_choice_dialog(find_popup,
					 MSG_QUESTION,
					 settings->x_pos, settings->y_pos,
					 NULL,
#ifndef MOTIF
					 "do-search-restart",
#endif
					 NULL, NULL, /* no pre_callbacks */
					 "Yes", search_restart, (XtPointer)settings,
					 "Cancel", message_search_ended, (XtPointer)settings,
					 "Searched %s of file without finding the pattern.\n"
					 "Start again from the %s of the file?",
					 settings->direction == SEARCH_DOWN
					 ? "to end" : "to beginning",
					 settings->direction == SEARCH_DOWN
					 ? "beginning" : "end");
	    TRACE_GUI((stderr, "message_window: %p\n", (void *)settings->message_window));
	    /* notreached */
	    break;
	}
    } /* for(;;) */
}

void
search_dvi(XtPointer arg)
{
    struct search_settings *settings = (struct search_settings *)arg;
    struct search_info *searchinfo = settings->searchinfo;
    
#if HAVE_REGEX_H
    static regex_t regex;
#endif /* HAVE_REGEX_H */
    
    /* a mapping of page numbers to index positions in w_info->txt_buf,
       for the 2 pages that we scanned */
    static struct page_mapping page_mapping[2] = { { -1, -1 }, { -1, -1 } };
    
    static struct word_info w_info = { NULL, 0, 0, NULL, 0, 0, NULL, NULL, 0, False, False, False };

    static time_t dvi_time_bak = 0;
    static char *searchterm_bak = NULL;

    static const char *text_encoding = NULL;

    static Boolean case_sensitive_bak = False;
    static Boolean ignore_hyphens_bak = False;
    static Boolean ignore_linebreaks_bak = False;
    static searchDirectionT direction_bak = SEARCH_UNINITIALIZED;
    /* from_pos_bak is needed to mark the start of the match when switching from
       search down to search backwards, where we want to jump to the previous match,
       so we need the start of the current match again (and down search sets
       searchinfo->from_pos = searchinfo->to_pos) */
    static int from_pos_bak = -1;
    /*      int curr_page; */

    Boolean reinit = False;
    Boolean recompile_regexp = False;
    Boolean adjust_hyphen_offset = False;
    
    /* prevent multiple invocations while still busy searching */
    if (searchinfo->locked) {
	TRACE_FIND((stderr, "LOCKED"));
	return;
    }
    searchinfo->locked = True;

    if (dvi_time_bak == 0) { /* first invocation */
	case_sensitive_bak = settings->case_sensitive;
	ignore_hyphens_bak = settings->ignore_hyphens;
	ignore_linebreaks_bak = settings->ignore_linebreaks;
	dvi_time_bak = globals.dvi_file.time;
    }
    if (globals.dvi_file.time > dvi_time_bak) {
	dvi_time_bak = globals.dvi_file.time;
	reinit = True;
    }

    m_match_page = -1;
    
    if (raise_message_windows()) { /* any popups user still needs to deal with? */
	TRACE_GUI((stderr, "Still open message windows to deal with, returning ..."));
	searchinfo->locked = False;
	return;
    }

    ASSERT(settings->term != NULL, "settings->term mustn't be NULL!");
    if (strlen(settings->term) == 0) {
	if (settings->isearchterm) {
	    settings->searchinfo->locked = False;
	    return;
	}
	
	positioned_popup_message(XtNameToWidget(globals.widgets.top_level, "*find_popup"),
				 MSG_ERR,
				 settings->x_pos, settings->y_pos,
				 NULL, "Empty search term");
	searchinfo->locked = False;
	return;
    }
    
    TRACE_FIND((stderr, "dvi_search: Searching for |%s| from page %d", settings->term, settings->from_page));
    TRACE_FIND((stderr, "  settings: down = %d, re = %d, case = %d",
		settings->direction, settings->use_regexp, settings->case_sensitive));

    if (m_changed_page) {
	m_changed_page = False;
	reinit = True;
    }
    
    /* initialize text_encoding */
    if (text_encoding == NULL) {
	text_encoding = get_text_encoding();
    }
    
    /* first call to this routine, or search term changed:
       Re-initialize the utf-8 representation and the regexp */
    if (settings->utf8_term == NULL
	|| searchterm_bak == NULL
	|| strcmp(settings->term, searchterm_bak) != 0) {

	if (!reinit_searchterm(settings, text_encoding)) {
	    searchinfo->locked = False;
	    return;
	}

	free(searchterm_bak);
	searchterm_bak = xstrdup(settings->term);
	recompile_regexp = True;
    }

    if (strlen(settings->utf8_term) == 0) {
	positioned_popup_message(XtNameToWidget(globals.widgets.top_level, "*find_popup"),
				 MSG_WARN,
				 settings->x_pos, settings->y_pos,
				 NULL, "Search term is empty after UTF-8 conversion!");
	searchinfo->locked = False;
	return;
    }

    if (direction_bak != settings->direction && direction_bak != SEARCH_UNINITIALIZED
	&& searchinfo->from_pos != INT_MAX && searchinfo->from_pos != -1) {
	TRACE_FIND((stderr, "changed direction! from_pos: %d", searchinfo->from_pos));
	if (settings->direction == SEARCH_DOWN) {
	    searchinfo->from_pos = searchinfo->to_pos;
	    TRACE_FIND((stderr, "DOWN; new from_pos: %d", searchinfo->from_pos));
	}
	else if (settings->direction == SEARCH_UP) {
	    if (from_pos_bak != -1) {
		searchinfo->from_pos = from_pos_bak;
	    }
	    else {
		searchinfo->from_pos = 0;
	    }
	    TRACE_FIND((stderr, "UP; new from_pos: %d", searchinfo->from_pos));
	}
    }
    direction_bak = settings->direction;

    /* If one of the settings for case, hyphens ore linebreaks has
     * changed, we need to rescan the page to undo lowercasing,
     * hyphenation or linebreak removal in w_info, but preserve
     * searchinfo->from_pos since user will want to find next match.
     */
    if (case_sensitive_bak != settings->case_sensitive
	|| ignore_hyphens_bak != settings->ignore_hyphens
	|| ignore_linebreaks_bak != settings->ignore_linebreaks) {

	if (ignore_hyphens_bak != settings->ignore_hyphens)
	    adjust_hyphen_offset = True;
	case_sensitive_bak = settings->case_sensitive;
	ignore_hyphens_bak = settings->ignore_hyphens;
	ignore_linebreaks_bak = settings->ignore_linebreaks;
	
	reset_info(&w_info);

	/* adjust from_pos if it's on second page (we'll rescan it as first page) */
	if (searchinfo->from_pos >= page_mapping[0].offset)
	    searchinfo->from_pos -= page_mapping[0].offset;

	TRACE_FIND((stderr, "from_pos: %d", searchinfo->from_pos));
	page_mapping[0].offset = page_mapping[1].offset = page_mapping[0].pageno = page_mapping[1].pageno = -1;
	/* also need to recompile regexp, and undo lowercasing of search term */
	if (!reinit_searchterm(settings, text_encoding))
	    return;
	recompile_regexp = True;
    }
    
    if (reinit) { /* file changed, or on different page: re-initialize scan info */
	TRACE_FIND((stderr, "re-initializing scan info!"));
	page_mapping[0].offset = page_mapping[1].offset = page_mapping[0].pageno = page_mapping[1].pageno = -1;

	/* re-initialize info */
	reset_info(&w_info);
	if (settings->direction == SEARCH_DOWN)
	    searchinfo->from_pos = searchinfo->to_pos = 0;
	else
	    settings->searchinfo->from_pos = settings->searchinfo->to_pos = INT_MAX;
    }

    TRACE_FIND((stderr, "from_pos initialized with: %d", settings->searchinfo->from_pos));
    
    if (settings->use_regexp && recompile_regexp) {
#if HAVE_REGEX_H
	if (!do_recompile_regexp(settings, &regex))
	    return;
#else
	warn_no_regex();
#endif /* HAVE_REGEX_H */
    }

    /* do it */
    search_over_pages(settings, page_mapping, &w_info, &from_pos_bak, adjust_hyphen_offset
#if HAVE_REGEX_H
		      , &regex
#endif
		      );
}

static void cb_isearch(Widget w, XtPointer closure, XEvent *event, Boolean *cont);
	
static void
do_isearch_cancel(struct search_settings *settings)
{
    static const char default_key_translations[] =
	"\"0\":digit(0)\n"
	"\"1\":digit(1)\n"
	"\"2\":digit(2)\n"
	"\"3\":digit(3)\n"
	"\"4\":digit(4)\n"
	"\"5\":digit(5)\n"
	"\"6\":digit(6)\n"
	"\"7\":digit(7)\n"
	"\"8\":digit(8)\n"
	"\"9\":digit(9)\n"
	/* 		"\"-\":minus()\n" */
	"<Motion>:motion()\n";
    
    static const char default_mouse_translations[] =
	"<BtnUp>:release()";

    
    statusline_info(STATUS_SHORT, "I-search stopped.");

    free(settings->isearchterm);
    settings->isearchterm = NULL;
    settings->searchinfo->from_pos = settings->searchinfo->to_pos = 0;
    settings->from_page = current_page;
    erase_match_highlighting(m_info, True);

	

    /* restore translations */
    XtOverrideTranslations(globals.widgets.top_level, XtParseTranslationTable(base_key_translations));
    XtOverrideTranslations(globals.widgets.top_level, XtParseTranslationTable(default_key_translations));
    XtOverrideTranslations(globals.widgets.top_level, XtParseTranslationTable(base_mouse_translations));
    XtOverrideTranslations(globals.widgets.top_level, XtParseTranslationTable(default_mouse_translations));

    XtOverrideTranslations(globals.widgets.draw_widget, XtParseTranslationTable(base_key_translations));
    XtOverrideTranslations(globals.widgets.draw_widget, XtParseTranslationTable(default_key_translations));
    XtOverrideTranslations(globals.widgets.draw_widget, XtParseTranslationTable(base_mouse_translations));
    XtOverrideTranslations(globals.widgets.draw_widget, XtParseTranslationTable(default_mouse_translations));

    XtOverrideTranslations(globals.widgets.clip_widget, XtParseTranslationTable(base_key_translations));
    XtOverrideTranslations(globals.widgets.clip_widget, XtParseTranslationTable(default_key_translations));
    XtOverrideTranslations(globals.widgets.clip_widget, XtParseTranslationTable(base_mouse_translations));
    XtOverrideTranslations(globals.widgets.clip_widget, XtParseTranslationTable(default_mouse_translations));

    if (resource.main_translations != NULL) {
	XtOverrideTranslations(globals.widgets.draw_widget, XtParseTranslationTable(resource.main_translations));
	XtOverrideTranslations(globals.widgets.clip_widget, XtParseTranslationTable(resource.main_translations));
    }

    XtRemoveEventHandler(globals.widgets.top_level, KeyPressMask|KeyReleaseMask, False,
			 cb_isearch, (XtPointer)settings);
    XtRemoveEventHandler(globals.widgets.draw_widget, KeyPressMask|KeyReleaseMask, False,
			 cb_isearch, (XtPointer)settings);
    XtRemoveEventHandler(globals.widgets.clip_widget, KeyPressMask|KeyReleaseMask, False,
			 cb_isearch, (XtPointer)settings);
}

static void
cb_isearch(Widget w, XtPointer closure, XEvent *event, Boolean *cont)
{
    struct search_settings *settings = (struct search_settings *)closure;
    	
    UNUSED(w);
    UNUSED(cont);
	    
    if (settings->isearchterm == NULL)
	settings->isearchterm = xstrdup("");
    
    if (event->type == KeyPress) {
	char buf[48];
	KeySym keysym;
	int count;
	/* temporarily disable control mask; this gives better results with XLookupString() */
	const unsigned int save_state = event->xkey.state;
	/* 	event->xkey.state &= ~ControlMask; */
	count = XLookupString(&(event->xkey), buf, 48, &keysym, NULL);
	event->xkey.state = save_state;
	if (count > 0) {
	    /* Ctrl-g and Ctrl-f continue isearch and
	       switch to the search window, respectively */
	    if (event->xkey.state & ControlMask) {
		if (keysym == XK_g) {
		    settings->term = settings->isearchterm;
		    isearch_start(); /* restart search */
		    return;
		}
		else if (keysym == XK_f) {
		    static char *str = NULL;
		    if (str != NULL) {
			free(str);
		    }
		    str = xstrdup(settings->isearchterm);
		    do_isearch_cancel(settings);
		    TRACE_FIND((stderr, "restarting search: %s", str));
		    dvi_find_string(str, False);
		    return;
		}
	    }
	    if (keysym == XK_Escape) {
		do_isearch_cancel(settings);
		return;
	    }
	    if (keysym == XK_BackSpace || keysym == XK_Delete) {
		size_t len = strlen(settings->isearchterm);
		if (len > 0) {
		    settings->isearchterm = xrealloc(settings->isearchterm, len);
		    settings->isearchterm[len - 1] = '\0';
		    /* statusline_info(STATUS_FOREVER, "I-search (ESC to exit): %s", settings->posix_term); */
		}
		else {
		    xdvi_bell();
		}
	    }
            else if (keysym >= XK_Shift_L && keysym <= XK_Hyper_R) {
		fprintf(stderr, "keysym %ld: Modifier %s\n", keysym, XKeysymToString(keysym));
	    }
	    else if (keysym == XK_Return || keysym == XK_KP_Enter || keysym == XK_Linefeed) { /* TODO */
		settings->term = settings->isearchterm;
		isearch_start(); /* restart search */
		return;
		/* statusline_info(STATUS_FOREVER, "I-search (ESC to exit): %s (next match)", settings->isearchterm); */
	    }
	    else if ((keysym >= XK_KP_Space && keysym <= XK_KP_9)
		     || (keysym >= XK_space && keysym <= XK_asciitilde)
		     || (keysym >= XK_exclamdown && keysym <= XK_ydiaeresis)
		     || (keysym >= XK_F1 && keysym <= XK_F35)) {
		settings->isearchterm = xstrcat(settings->isearchterm, buf);
		/* statusline_info(STATUS_FOREVER, "I-search (ESC to exit): %s", settings->posix_term); */
            }
	    else { /* TODO: abort search? */
		xdvi_bell();
		TRACE_FIND((stderr, "keysym %ld: %s is not handled", keysym, XKeysymToString(keysym)));
	    }
	
	    if (settings->isearchterm[0] != '\0') {
		/* 		const char *prefix = ""; */
		/* 		fprintf(stderr, "cb_isearch called!\n"); */
		/* 		if (settings->wrapcnt > 0) */
		/* 		    prefix = "Wrapped "; */
		/* 		statusline_info(STATUS_FOREVER, "%sI-search (ESC to exit): %s", prefix, settings->isearchterm); */
		settings->from_page = current_page;
		settings->message_window = 0;
		/* 	settings->wrapcnt = 0; */
		settings->term = settings->isearchterm;
		search_dvi((XtPointer)settings);
	    }
	}
    }

}
	    
#ifdef MOTIF
#define XTranslations XmNtranslations
#else
#define XTranslations XtNtranslations
#endif
    
/* static void */
/* isearch_cancel(Widget w, XEvent *event, String *params, Cardinal *num_params) */
/* { */
/*     struct search_settings *settings = NULL; */
/*     void *ptr; */
    
/*     UNUSED(w); */
/*     UNUSED(event); */
    
/*     if (*num_params < 1) { */
/* 	XDVI_WARNING((stderr, "Wrong argument number (%d) in callback!", *num_params)); */
/* 	return; */
/*     } */
/*     sscanf(*params, "%p", &ptr); */
/*     settings = (struct search_settings *)ptr; */
/*     do_isearch_cancel(settings); */
/* } */


/* static XtActionsRec isearch_actions[] = { */
/*     {"CancelIsearch",		isearch_cancel }, */
/* }; */

void isearch_start(void)
{
    /*     char *ptr = NULL; */
    XtTranslations empty_trans;
    static struct search_settings settings = {
	NULL, NULL, NULL,
	False, False, False, False,
	False, 0,
	NULL, /* isearchterm */
	SEARCH_DOWN, NULL,
	0, 0,
	0, 0, 0,
	0, NULL
    };
    static struct search_info searchinfo = {
	False, False, False,
	0, 0, 0, 0
    };
    static int wrapcnt_bak = 0;
    
    settings.searchinfo = &searchinfo;

    if (settings.isearchterm) { /* already running a search, jump to next match */
	settings.searchinfo->from_pos = settings.searchinfo->to_pos;
	if (settings.wrapcnt > wrapcnt_bak) { /* search wrapped */
	    wrapcnt_bak = settings.wrapcnt;
	    search_restart((XtPointer)&settings);
	}
	else {
	    wrapcnt_bak = settings.wrapcnt;
	    search_dvi((XtPointer)&settings);
	}
	return;
    }
    
    settings.wrapcnt = 0;
    
    /*     ptr = get_string_va("<Key>osfCancel:CancelIsearch(%p)", (void *)&settings); */
    /*     empty_trans = XtParseTranslationTable(ptr); */
    empty_trans = XtParseTranslationTable("");
    /*     free(ptr); */
    /*     XtAddActions(isearch_actions, XtNumber(isearch_actions)); */

    statusline_info(STATUS_FOREVER, "I-search (ESC to exit): ");
    XtVaSetValues(globals.widgets.top_level, XTranslations, empty_trans, NULL);
    XtAddEventHandler(globals.widgets.top_level, KeyPressMask|KeyReleaseMask, False,
		      cb_isearch, (XtPointer)&settings);
    
    XtVaSetValues(globals.widgets.draw_widget, XTranslations, empty_trans, NULL);
    XtAddEventHandler(globals.widgets.draw_widget, KeyPressMask|KeyReleaseMask, False,
		      cb_isearch, (XtPointer)&settings);
    
    XtVaSetValues(globals.widgets.clip_widget, XTranslations, empty_trans, NULL);
    XtAddEventHandler(globals.widgets.clip_widget, KeyPressMask|KeyReleaseMask, False,
		      cb_isearch, (XtPointer)&settings);
}