File: wnb

package info (click to toggle)
wordnet 1%3A3.0-35
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 25,320 kB
  • sloc: sh: 10,763; ansic: 5,881; yacc: 758; ruby: 634; lex: 417; python: 317; makefile: 137
file content (1810 lines) | stat: -rwxr-xr-x 56,805 bytes parent folder | download | duplicates (5)
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
#!/bin/sh 
# the following line is evaluated by sh but ignored by tcl \
/usr/lib/wordnet/wishwn "$0" "$@" &
# the following line is evaluated by sh but ignored by tcl \
exec true
# the preceding lines make this script self-executing on unix systems

##########################################################################
#                                                                        # 
#  A Tcl/Tk interface for WordNet                                        # 
#  by David Slomin (dgslomin@princeton.edu) - 6/97                       # 
#  based upon the X-Windows version by Brian Gustafson - 5/91            # 
#                                                                        # 
##########################################################################

if {$tcl_platform(platform) == "windows"} {
  package require registry 1.1
}

### Default Configuration

set showcontextualhelp 0 
set showglosses 1
set wordwrap 1
set showfileinfo 0
set showbyteoffset 0
set showsenseflag 0
set maxhistorylength 10
set fontname times
set fontsize 2
set fontsizelist {100 120 140 180 240}

### Indentation constants

# "->"                         # "=>"
set fontpt1offset(times,1) 20; set fontpt2offset(times,1) 20
set fontpt1offset(times,2) 20; set fontpt2offset(times,2) 20
set fontpt1offset(times,3) 26; set fontpt2offset(times,3) 26
set fontpt1offset(courier,1) 23; set fontpt2offset(courier,1) 23
set fontpt1offset(courier,2) 28; set fontpt2offset(courier,2) 28
set fontpt1offset(courier,3) 34; set fontpt2offset(courier,3) 34
set fontpt1offset(helvetica,1) 19; set fontpt2offset(helvetica,1) 19
set fontpt1offset(helvetica,2) 24; set fontpt2offset(helvetica,2) 24
set fontpt1offset(helvetica,3) 27; set fontpt2offset(helvetica,3) 27

# "HAS PART:"		      # "PART OF:"                 
set fonthpoffset(times,1) 66; set fontpooffset(times,1) 58
set fonthpoffset(times,2) 77; set fontpooffset(times,2) 66
set fonthpoffset(times,3) 100; set fontpooffset(times,3) 87
set fonthpoffset(courier,1) 71; set fontpooffset(courier,1) 64
set fonthpoffset(courier,2) 91; set fontpooffset(courier,2) 82
set fonthpoffset(courier,3) 111; set fontpooffset(courier,3) 100
set fonthpoffset(helvetica,1) 70; set fontpooffset(helvetica,1) 62
set fonthpoffset(helvetica,2) 80; set fontpooffset(helvetica,2) 70
set fonthpoffset(helvetica,3) 102; set fontpooffset(helvetica,3) 90

# "HAS MEMBER:"               # "MEMBER OF:"
set fonthmoffset(times,1) 74; set fontmooffset(times,1) 81
set fonthmoffset(times,2) 85; set fontmooffset(times,2) 92
set fonthmoffset(times,3) 108; set fontmooffset(times,3) 118
set fonthmoffset(courier,1) 75; set fontmooffset(courier,1) 79
set fonthmoffset(courier,2) 96; set fontmooffset(courier,2) 102
set fonthmoffset(courier,3) 118; set fontmooffset(courier,3) 124
set fonthmoffset(helvetica,1) 77; set fontmooffset(helvetica,1) 84
set fonthmoffset(helvetica,2) 87; set fontmooffset(helvetica,2) 95
set fonthmoffset(helvetica,3) 111; set fontmooffset(helvetica,3) 121

# "HAS SUBSTANCE:"             # "SUBSTANCE OF:"
set fonthsoffset(times,1) 105; set fontsooffset(times,1) 97
set fonthsoffset(times,2) 124; set fontsooffset(times,2) 113
set fonthsoffset(times,3) 157; set fontsooffset(times,3) 146
set fonthsoffset(courier,1) 106; set fontsooffset(courier,1) 98
set fonthsoffset(courier,2) 136; set fontsooffset(courier,2) 127
set fonthsoffset(courier,3) 166; set fontsooffset(courier,3) 155
set fonthsoffset(helvetica,1) 111; set fontsooffset(helvetica,1) 103
set fonthsoffset(helvetica,2) 127; set fontsooffset(helvetica,2) 117
set fonthsoffset(helvetica,3) 166; set fontsooffset(helvetica,3) 156

# " "			     # "[0-9]"
set fontspoffset(times,1) 3; set fontNUMoffset(times,1) 6
set fontspoffset(times,2) 3; set fontNUMoffset(times,2) 7
set fontspoffset(times,3) 4; set fontNUMoffset(times,3) 10
set fontspoffset(courier,1) 7; set fontNUMoffset(courier,1) 8
set fontspoffset(courier,2) 9; set fontNUMoffset(courier,2) 11
set fontspoffset(courier,3) 11; set fontNUMoffset(courier,3) 13
set fontspoffset(helvetica,1) 4; set fontNUMoffset(helvetica,1) 7
set fontspoffset(helvetica,2) 4; set fontNUMoffset(helvetica,2) 8
set fontspoffset(helvetica,3) 5; set fontNUMoffset(helvetica,3) 9

# ". "
set fontsp2offset(times,1) 6;
set fontsp2offset(times,2) 6;
set fontsp2offset(times,3) 7;
set fontsp2offset(courier,1) 12;
set fontsp2offset(courier,2) 15;
set fontsp2offset(courier,3) 19;
set fontsp2offset(helvetica,1) 7;
set fontsp2offset(helvetica,2) 7;
set fontsp2offset(helvetica,3) 11;

set labonly 0
set version "3.0"

if {$tcl_platform(platform) == "unix"} {
   if {[lsearch -exact [array names env] WNHOME] == -1} {
	set resourcedir "/usr/share/wordnet/wnres"
   } else {
	set resourcedir "$env(WNHOME)/lib/wnres"
   }
   set configfile "$env(HOME)/.wnrc"
   if [ file exists $configfile ] {
       source $configfile
   }
   set printcommand "lpr"
}
if {$tcl_platform(platform) == "windows"} {
    set UserRegPath "HKEY_CURRENT_USER\\Software\\WordNet\\3.0"
    if { [registry values $UserRegPath "wnrc" ] != "" } {
	eval [ registry get $UserRegPath "wnrc" ]
    }
    if { [registry values $UserRegPath "WNHome"] != "" } {
	 append resourcedir [registry get $UserRegPath "WNHome"] "/lib/wnres"
    } else {
    	 set MachineRegPath "HKEY_LOCAL_MACHINE\\Software\\WordNet\\3.0"
    	 if { [registry values $MachineRegPath "WNHome" ] != "" } {
     	    append resourcedir [registry get $MachineRegPath "WNHome"] "/lib/wnres"
	 } else {
          set resourcedir "C:/Program Files/WordNet/3.0/lib/wnres"
       }
    }
}

### Startup

wm title . "WordNet 3.0 Browser"

### Primary Functions

proc displayvalidsearchmenus {searchword} {
   global posmenu
   blackout 1
   set gotone 0
   pack forget .wordframe.overview
   for {set posnumber 1} {$posnumber <= 4} {incr posnumber} {
      set pos [lindex {noun verb adj adv} [expr $posnumber - 1]]
      pack forget .posmenubar.$pos
      set bitfield [findvalidsearches [fixword $searchword] $posnumber]
      if {$bitfield != 0} {
         pack \
            .posmenubar.$pos \
            -side left \
            -padx 2 \
            -pady 2 \
            -before .posmenubar.senselabel
         .posmenubar.$pos.menu delete 0 end
         foreach line $posmenu($posnumber) {
            set label [lindex $line 0]
            regsub -nocase "this" $label $searchword label
            set searchtypenumber [lindex $line 1]
            set abssearchtypenumber [expr abs($searchtypenumber)]
            set action "\
               searchanddisplay \"$searchword\" \"\$g_senses\" $posnumber \
                  $searchtypenumber; \
               history_add \"$searchword\" \"\$g_senses\" $posnumber \
                  $searchtypenumber; \
               pack .wordframe.overview \
                  -side right \
                  -padx 5 \
                  -pady 1
            "
            if {[expr $bitfield & [bit $abssearchtypenumber]]} {
               .posmenubar.$pos.menu add command \
                  -label $label \
                  -command $action
            }
         }
         set gotone 1
      }
   }
   if {!$gotone} {
      .statusbar.status configure \
         -text "No matches found."
   }
   blackout 0
   return $gotone
}

proc displayoverview {searchword} {
   global showfileinfo
   global showbyteoffset
   global showsenseflag
   global wordwrap
   global fontNUMoffset
   global fontsize
   global fontname
   global fontsp2offset

   set fontNUMsize $fontNUMoffset($fontname,$fontsize)	
   set fontsp2size $fontsp2offset($fontname,$fontsize)

   if {$showfileinfo == 2} {fileinfo 1} else {fileinfo 0}
   if {$showbyteoffset == 2} {byteoffset 1} else {byteoffset 0}
   if {$showsenseflag == 2} {senseflag 1} else {senseflag 0}
   blackout 1
   .results.text configure \
      -state normal
   .results.text delete 1.0 end
   pack forget .wordframe.overview
   set gotone 0
   for {set posnumber 1} {$posnumber <= 4} {incr posnumber} {
      set bitfield [findvalidsearches [fixword $searchword] $posnumber]
      if {$bitfield != 0} {
         # The 31 in the following line should correspond to the value of
         # OVERVIEW defined in wnconsts.h in the WordNet library
         set buf [search [fixword $searchword] $posnumber 31 -1]
         set morphedword ""
	 set numspaces 0
	 set iter 0
         set buflines [split $buf "\n"]
         foreach line $buflines {
	    .results.text mark set begofline \
		[.results.text index "end - 2 char"]
            .results.text insert end "$line\n"
            regexp -nocase -- \
               "The (noun|verb|adj|adv) (.*) has \[0-9\]+ senses?" \
               $line dummy dummy morphedword
            set index [string first " --" $line]
            if {$index != -1} {
               set line [string range $line 0 [expr $index - 1]]
            }
            if {[regexp -indices -nocase -- \
               "\[\})>,\] ($morphedword)\[0-9\]*(#\[0-9\]+)?(,|\$)" \
               $line dummy indices]} {
               .results.text tag add overviewhighlight \
                  "end - 2 lines linestart + [lindex $indices 0] chars" \
                  "end - 2 lines linestart + [expr \
                     [lindex $indices 1] + 1] chars"
            } elseif {[regexp -indices -nocase -- \
               "\[0-9\]+\. ($morphedword)\[0-9\]*(#\[0-9\]+)?(,|\$)" \
               $line dummy indices]} {
               .results.text tag add overviewhighlight \
                  "end - 2 lines linestart + [lindex $indices 0] chars" \
                  "end - 2 lines linestart + [expr \
                     [lindex $indices 1] + 1] chars"
	    }
	    set indent [string first "." $line]
	    if {$indent != -1} {
		set numspaces [expr $indent * $fontNUMsize + $fontsp2size]
            } else {set numspaces 0}
	    .results.text mark set endofline [.results.text index \
	     "end - 1 char"]
	    .results.text tag configure indent$posnumber$iter \
		-lmargin2 $numspaces
            .results.text tag add indent$posnumber$iter \
		  begofline endofline
	    .results.text mark set begofline [.results.text index \
	     "end - 2 char"]
	    incr iter
         }
         .results.text delete "end - 1 lines" end
         set gotone 1
      }
   }
   .results.text configure \
      -state disabled
   if {$gotone} { 
      .statusbar.status configure \
         -text "Overview of $searchword" 
   } 
   blackout 0
   setwordwrap $wordwrap
   focus .results.text
   return $gotone
}

proc searchanddisplay {searchword senses posnumber searchtypenumber} {
   global posmenu
   global showcontextualhelp
   global showglosses
   global showfileinfo
   global showbyteoffset
   global showsenseflag
   blackout 1
   glosses $showglosses
   if {$showfileinfo > 0} {fileinfo 1} else {fileinfo 0}
   if {$showbyteoffset > 0} {byteoffset 1} else {byteoffset 0}
   if {$showsenseflag > 0} {senseflag 1} else {senseflag 0}
   set longpos [lindex {noun verb adjective adverb} [expr $posnumber - 1]]
   .results.text configure \
      -state normal
   .results.text delete 0.0 end
   .statusbar.status configure \
      -text "Searching... (press escape to abort)"
   update idletasks
   if {$showcontextualhelp} {
      foreach line $posmenu($posnumber) {
         if {[lindex $line 1] == $searchtypenumber} {
            set linenumber [lsearch $posmenu($posnumber) $line]
         }
      }
      .results.text insert end "\n[contextualhelp $posnumber $linenumber]"
      .results.text mark set endofhelp [.results.text index "end - 1 char"]
      .results.text tag add helpstyle 1.0 endofhelp
   }
   foreach sense [getsenselist $senses] {

	global fontsize

	global fontpt1offset
	global fontpt2offset
	global fonthpoffset
	global fontpooffset
	global fonthmoffset
	global fontmooffset
	global fonthsoffset
	global fontsooffset
	global fontspoffset

	global fontname
	global wordwrap

      if {$wordwrap} {	  
	set searchlines [split [search [fixword $searchword] $posnumber \
         $searchtypenumber $sense] "\n"]
	.results.text mark set begofline [.results.text index "end - 2 char"]

	### Set indentation levels ###	

	set fontpt1size $fontpt1offset($fontname,$fontsize)
	set fontpt2size $fontpt2offset($fontname,$fontsize)
	set fonthpsize $fonthpoffset($fontname,$fontsize)
	set fontposize $fontpooffset($fontname,$fontsize)
	set fonthmsize $fonthmoffset($fontname,$fontsize)
	set fontmosize $fontmooffset($fontname,$fontsize)
	set fonthssize $fonthsoffset($fontname,$fontsize)
	set fontsosize $fontsooffset($fontname,$fontsize)
	set fontspsize $fontspoffset($fontname,$fontsize)

        .statusbar.status configure \
            -text "Formatting... (press escape to abort)"

	set iter 0
	set morphedword ""
	set mword ""
	foreach i $searchlines {
	    .results.text insert end $i\n
	    .results.text mark set endofline [.results.text index \
	     "end - 1 char"]

# added by RIT
	    regexp -nocase -- \
		"\[0-9\]+ (senses|sense) of (.+)" \
		    $i dummy dummy mword
	    set morphedword [string trimright $mword]
   	    set index [string first " --" $i]
	    set schar [string first " " $i]
	    if {$index != -1 && $schar != 0} {
		set line [string range $i 0 [expr $index - 1]]
		if {[regexp -indices -nocase -- \
			 "(\[\.\})>,\] |^|, )($morphedword)\[0-9\]*(#\[0-9\]+)?( \[(\]|,|$\)" \
		    $line dummy dummy indices]} {
		    .results.text tag add overviewhighlight \
			"end - 2 lines linestart + [lindex $indices 0] chars" \
			"end - 2 lines linestart + [expr \
                     [lindex $indices 1] + 1] chars"
		}
            }

	    set numspaces 0
	    set idx [string first "->" $i]
	    if {$idx != -1} {set numspaces \
	       [expr $fontpt1size + $idx * $fontspsize - 1]} \
	     else {set idx [string first "=>" $i]
	      if {$idx != -1} {set numspaces \
		 [expr $fontpt2size + $idx * $fontspsize]} \
              else {set idx [string first "HAS PART:" $i]
	       if {$idx != -1} {set numspaces \
		  [expr $fonthpsize + $idx * $fontspsize - 1]} \
	       else {set idx [string first "PART OF:" $i]
	        if {$idx != -1} {set numspaces \
		   [expr $fontposize + $idx * $fontspsize - 1]} \
	        else {set idx [string first "HAS MEMBER:" $i]
	         if {$idx != -1} {set numspaces \
	            [expr $fonthmsize + $idx * $fontspsize - 1]} \
	         else {set idx [string first "MEMBER OF:" $i]
	          if {$idx != -1} {set numspaces \
	             [expr $fontmosize + $idx * $fontspsize - 1]} \
	          else {set idx [string first "HAS SUBSTANCE:" $i]
		   if {$idx != -1} {set numspaces \
		      [expr $fonthssize + $idx * $fontspsize]} \
	           else {set idx [string first "SUBSTANCE OF:" $i]
	            if {$idx != -1} {set numspaces \
	               [expr $fontsosize + $idx * $fontspsize]} \	    
	     } } } } } } }

	    .results.text tag configure indentstyle$iter -lmargin2 $numspaces
	    .results.text tag add indentstyle$iter begofline endofline
	    .results.text mark set begofline [.results.text index \
	     "end - 2 char"]
	    incr iter
	}
      } else {
	.results.text insert end [search [fixword $searchword] $posnumber \
         $searchtypenumber $sense]
      }

      setwordwrap $wordwrap
   }
   foreach line $posmenu($posnumber) {
      if {[lindex $line 1] == $searchtypenumber} {
         set label "\"[lindex $line 0]\" search for $longpos \"$searchword\""
         eval [lindex $line 2]
         break
      }
   }
   .results.text configure \
      -state disabled
   .statusbar.status configure \
      -text $label
   blackout 0
} 

proc history_add {searchword senses posnumber searchtypenumber} {
   global posmenu
   global maxhistorylength

   if {$posnumber == 0} {
      set label "$searchword - Overview"
      set action " \
         set g_searchword \"$searchword\"; \
         set g_senses \"\"; \
         displayvalidsearchmenus \"$searchword\"; \
         displayoverview \"$searchword\"; \
         .posmenubar.label configure \
            -text \"Searches for $searchword:\" \
      "
   } else {
      set longpos [lindex {Noun Verb Adjective Adverb} [expr $posnumber - 1]]
      foreach line $posmenu($posnumber) {
         if {[lindex $line 1] == $searchtypenumber} {
            if {$senses == ""} {
               set label "$searchword - $longpos / [lindex $line 0]"
            } else {
               set label \
                  "$searchword - $longpos / [lindex $line 0] / $senses"
            }
            break
         }
      }
      set action " \
         set g_searchword \"$searchword\"; \
         set g_senses \"$senses\"; \
         displayvalidsearchmenus \"$searchword\"; \
         searchanddisplay \"$searchword\" \"$senses\" $posnumber \
            $searchtypenumber; \
         pack .wordframe.overview \
            -side right \
            -padx 5 \
            -pady 1; \
         .posmenubar.label configure \
            -text \"Searches for $searchword:\" \
      "
   }
   .menubar.history.menu insert 0 command \
      -label $label \
      -command $action
   if {[.menubar.history.menu index end] == $maxhistorylength} {
      .menubar.history.menu delete end
   }
}

proc saveoptions { } {

    global tcl_platform
    global UserRegPath
    global showcontextualhelp
    global showglosses
    global wordwrap
    global showfileinfo
    global showbyteoffset
    global showsenseflag
    global maxhistorylength
    global fontname
    global fontsize
    global fontsizelist
    global configfile

# Under Unix, write options settings to ".wnrc" file in user's 
# home directory.  
# Under Windows, write to "wnrc" key in registry.

    if {$tcl_platform(platform) == "unix"} {
	set c [open $configfile w]
	puts $c "set showcontextualhelp $showcontextualhelp"
	puts $c "set showglosses $showglosses"
	puts $c "set wordwrap $wordwrap"
	puts $c "set showfileinfo $showfileinfo"
	puts $c "set showbyteoffset $showbyteoffset"
	puts $c "set showsenseflag $showsenseflag"
	puts $c "set maxhistorylength $maxhistorylength"
	puts $c "set fontname $fontname"
	puts $c "set fontsize $fontsize"
	puts $c "set fontsizelist \{ $fontsizelist \}"
    } elseif {$tcl_platform(platform) == "windows"} {
	set c ""
	append c "set showcontextualhelp $showcontextualhelp\n"
	append c "set showglosses $showglosses\n"
	append c "set wordwrap $wordwrap\n"
	append c "set showfileinfo $showfileinfo\n"
	append c "set showbyteoffset $showbyteoffset\n"
	append c "set showsenseflag $showsenseflag\n"
	append c "set maxhistorylength $maxhistorylength\n"
	append c "set fontname $fontname\n"
	append c "set fontsize $fontsize\n"
	append c "set fontsizelist \{ $fontsizelist \}\n"
	registry set $UserRegPath "wnrc" $c
    }
}
 
proc showhelpwidget {w filename windowtitle} {
   if {[winfo exist .$w]} {raise .$w; return}
   toplevel .$w
   wm title .$w $windowtitle
   grid \
      [frame .$w.top \
         -relief raised \
         -borderwidth 1] \
      -row 0 \
      -column 0 \
      -sticky nsew
   grid \
      [frame .$w.buttons \
         -relief raised \
         -borderwidth 1] \
      -row 1 \
      -column 0 \
      -sticky nsew
   grid rowconfigure .$w 0 -weight 1
   grid rowconfigure .$w 1 -weight 0
   grid columnconfigure .$w 0 -weight 1
   text .$w.top.text \
      -wrap word \
      -relief sunken \
      -borderwidth 2 \
      -font -adobe-courier-medium-r-normal-*-*-120-*-*-*-*-*-* \
      -state disabled \
      -yscrollcommand ".$w.top.scrolly set" \
      -width 80 \
      -height 25 \
      -background White \
      -foreground Black
   scrollbar .$w.top.scrolly \
      -command ".$w.top.text yview" \
      -relief sunken \
      -width 12
   grid .$w.top.text \
      -row 0 \
      -column 0 \
      -sticky nsew
   grid .$w.top.scrolly \
      -row 0 \
      -column 1 \
      -sticky nsew
   grid rowconfigure .$w.top 0 -weight 1
   grid rowconfigure .$w.top 1 -weight 0
   grid columnconfigure .$w.top 0 -weight 1
   grid columnconfigure .$w.top 1 -weight 0
   pack \
      [button .$w.buttons.dismiss \
         -text "Dismiss" \
         -command "destroy .$w"] \
      -side top \
      -padx 1 \
      -pady 1
   .$w.top.text configure \
      -state normal
   set fileid [open $filename "r"]
   set filetext [read $fileid]
   close $fileid
   regsub -all ".\b" $filetext "" filetext
   .$w.top.text insert end $filetext
   .$w.top.text configure \
      -state disabled
}

proc printtext {whattoprint} {
   # The following line is a hack to get the passed argument to be
   # recognised in bound actions, like button presses.
   global printtext_whattoprint; set printtext_whattoprint $whattoprint
   global tcl_platform
   switch $tcl_platform(platform) {
      unix {
         global printcommand
         toplevel .printtxt
         wm title .printtxt "Print WordNet Results"
         wm transient .printtxt .
         scan [wm geometry .] "%dx%d+%d+%d" geom_h geom_w geom_x geom_y
         wm geometry .printtxt +[expr $geom_x+50]+[expr $geom_y+50]
         wm resizable .printtxt 0 0
         grab set .printtxt
         pack \
            [frame .printtxt.top] \
            [frame .printtxt.bottom] \
            -side top \
            -padx 10 \
            -pady 10
         pack \
            [label .printtxt.top.label \
               -text "Print command:"] \
            [entry .printtxt.top.entry \
               -textvariable printcommand \
               -background White \
               -foreground Black] \
            -side left
	 focus .printtxt.top.entry
         pack \
            [button .printtxt.bottom.print \
               -text "Print" \
               -command {
                  set fileId [open "| $printcommand" w]
                  switch $printtext_whattoprint {
                     "main" {
                        puts $fileId [.results.text get 1.0 end]
                     }
                     "grep" {
                        puts $fileId [join \
                           [.grepwidget.results.frame.list \
                           get 0 end] "\n"]
                     }
                     default {}
                  }
                  close $fileId
                  destroy .printtxt
               }] \
            [button .printtxt.bottom.cancel \
               -text "Cancel" \
               -command {
                  destroy .printtxt
               }] \
            -side left
         bind .printtxt <Return> {
            .printtxt.bottom.print flash
            .printtxt.bottom.print invoke
         }
      }
      windows {
         bell
      }
   }
}

proc savetext {whattosave} {
   # The following line is a hack to get the passed argument to be
   # recognised in bound actions, like button presses.
   global tcl_platform
   global savetext_whattosave; set savetext_whattosave $whattosave
   toplevel .saveas
   wm title .saveas "Save WordNet Results To File"
   wm transient .saveas .
   scan [wm geometry .] "%dx%d+%d+%d" geom_h geom_w geom_x geom_y
   wm geometry .saveas +[expr $geom_x+50]+[expr $geom_y+50]
   wm resizable .saveas 0 0
   grab set .saveas
   pack \
      [frame .saveas.top] \
      [frame .saveas.bottom] \
      -side top \
      -padx 10 \
      -pady 10
   pack \
      [label .saveas.top.label \
         -text "Filename:"] \
      [entry .saveas.top.entry \
         -textvariable filename \
         -background White \
         -foreground Black] \
      -side left
   focus .saveas.top.entry
   pack \
      [button .saveas.bottom.save \
         -text "Save" \
         -command { 
            if [file exists $filename] {
               toplevel .savewarning
               wm title .savewarning "Wordnet Warning"
               wm transient .savewarning .saveas
               scan [wm geometry .saveas] "%dx%d+%d+%d" \
                  geom_h geom_w geom_x geom_y
               wm geometry .savewarning +[expr $geom_x+50]+[expr $geom_y+50]
               wm resizable .savewarning 0 0
               grab set .savewarning
               pack \
                  [message .savewarning.message \
                     -text "The file \"$filename\" already exists.\
                        Choose \"append\" to add the new search results onto\
                        the end of the old ones.  Choose \"replace\" to\
                        discard the old search results and store the new ones\
                        in their place.  If you choose \"cancel,\" you will\
                        have the opportunity to select a different file." \
                     -width 300] \
                  [frame .savewarning.bottom] \
                  -side top \
                  -padx 10 \
                  -pady 10
               pack \
                  [button .savewarning.bottom.append \
                     -text "Append" \
                     -command {
                        set fileId [open $filename "a"]
                        switch $savetext_whattosave {
                           "main" {
                              puts $fileId [.results.text get 1.0 end]
                           }
                           "grep" {
                              puts $fileId [join \
                                 [.grepwidget.results.frame.list \
                                 get 0 end] "\n"]
                           }
                           default {}
                        }
                        close $fileId
                        destroy .saveas
                        destroy .savewarning
                     }] \
                  [button .savewarning.bottom.replace \
                     -text "Replace" \
                     -command {
                        set fileId [open $filename "w"]
                        switch $savetext_whattosave {
                           "main" {
                              puts $fileId [.results.text get 1.0 end]
                           }
                           "grep" {
                              puts $fileId [join \
                                 [.grepwidget.results.frame.list \
                                 get 0 end] "\n"]
                           }
                           default {}
                        }
                        close $fileId
                        destroy .saveas
                        destroy .savewarning
                     }] \
                  [button .savewarning.bottom.cancel \
                     -text "Cancel" \
                     -command {
                        destroy .savewarning
                     }] \
                  -side left
               bind .savewarning <Return> {
                  .savewarning.bottom.append flash
                  .savewarning.bottom.append invoke
               }
            } else {
               set fileId [open $filename "w"]
               switch $savetext_whattosave {
                  "main" {
                     puts $fileId [.results.text get 1.0 end]
                  }
                  "grep" {
                     puts $fileId [join [.grepwidget.results.frame.list \
                        get 0 end] "\n"]
                  }
                  default {}
               }
               close $fileId
               destroy .saveas
            }
         }] \
      [button .saveas.bottom.cancel \
         -text "Cancel" \
         -command {destroy .saveas}] \
      -side left
   bind .saveas <Return> {
      .saveas.bottom.save flash
      .saveas.bottom.save invoke
   }
}

proc setmaxhistorylength {} {
   global maxhistorylength
   global tcl_platform
   toplevel .histlen
   wm title .histlen "Set Maximum WordNet Browser History Length"
   wm transient .histlen .
   scan [wm geometry .] "%dx%d+%d+%d" geom_h geom_w geom_x geom_y
   wm geometry .histlen +[expr $geom_x+50]+[expr $geom_y+50]
   wm resizable .histlen 0 0
   grab set .histlen
   pack \
      [frame .histlen.f \
         -relief raised \
         -borderwidth 1] \
      -side top \
      -fill both \
      -expand true
   pack \
      [frame .histlen.f.top] \
      [frame .histlen.f.bottom] \
      -side top \
      -padx 10 \
      -pady 10
   pack \
      [label .histlen.f.top.label \
         -text "Maximum history length:"] \
      [entry .histlen.f.top.entry \
         -textvariable maxhistorylength \
         -background White \
         -foreground Black] \
      -side left
   focus .histlen.f.top.entry
   pack \
      [button .histlen.f.bottom.ok \
         -text "Ok" \
         -command {
            if {[.menubar.history.menu index end] >= $maxhistorylength} {
               .menubar.history.menu delete $maxhistorylength end
            }
            destroy .histlen
         }] \
      -side left
   bind .histlen <Return> {
      .histlen.f.bottom.ok flash
      .histlen.f.bottom.ok invoke
   }
}

proc showaboutbox {} {
   global resourcedir
   if {[winfo exist .aboutbox]} {raise .aboutbox; return}
   toplevel .aboutbox
   wm title .aboutbox "About WordNet Browser"
   wm resizable .aboutbox 0 0
   pack \
      [frame .aboutbox.top \
         -relief raised \
         -borderwidth 1] \
      [frame .aboutbox.bottom \
         -relief raised \
         -borderwidth 1] \
      -side top \
      -fill x \
      -ipadx 3 \
      -ipady 3
   pack \
      [frame .aboutbox.top.left \
         -relief flat] \
      [frame .aboutbox.top.right \
         -relief flat] \
      -side left \
      -fill x \
      -ipadx 3 \
      -ipady 3
   pack \
      [label .aboutbox.top.left.icon \
         -bitmap @$resourcedir/wn.xbm] \
      -side left \
      -padx 10 
   pack \
      [label .aboutbox.top.right.text1 \
         -anchor w \
         -justify left \
         -font "-adobe-helvetica-medium-r-*-*-*-180-*-*-*-*-*-*" \
         -text "WordNet Browser"] \
      [label .aboutbox.top.right.text2 \
         -anchor w \
         -justify left \
         -font "-adobe-helvetica-medium-r-*-*-*-120-*-*-*-*-*-*" \
         -text "A graphical interface to the\nWordNet online lexical\
            database."] \
      [label .aboutbox.top.right.text3 \
         -anchor w \
         -justify left \
         -font "-adobe-helvetica-medium-r-*-*-*-120-*-*-*-*-*-*" \
         -text "This Tcl/Tk version by David Slomin and Randee Tengi."] \
      [label .aboutbox.top.right.text4 \
         -anchor w \
         -justify left \
         -font "-adobe-helvetica-medium-r-*-*-*-120-*-*-*-*-*-*" \
         -text "Based upon an earlier X Window version by\nBrian Gustafson."] \
      [label .aboutbox.top.right.text5 \
         -anchor w \
         -justify left \
         -font "-adobe-helvetica-medium-r-*-*-*-120-*-*-*-*-*-*" \
         -text "Copyright 1991-2006\nPrinceton University Cognitive Science Lab"] \
      [label .aboutbox.top.right.text6 \
         -anchor w \
         -justify left \
         -font "-adobe-helvetica-medium-r-*-*-*-120-*-*-*-*-*-*" \
         -text "All Rights Reserved"] \
      -side top \
      -fill x
   pack \
      [button .aboutbox.bottom.ok \
         -text "Dismiss" \
         -command {destroy .aboutbox}] \
      -side top \
      -padx 3 \
      -pady 2
}

proc grepword {greptype} {
   global g_greptype; set g_greptype $greptype;
   global g_searchword
   global grepstr
   global tcl_platform

   if {[winfo exist .grepwidget]} {raise .grepwidget; return}

   set grepstr $g_searchword

   toplevel .grepwidget
   switch $g_greptype {
       "substring" { wm title .grepwidget "Substring search (grep)"}
       "ending" { wm title .grepwidget "Ending string search (grep)"}
   }
   grid \
      [frame .grepwidget.inputs \
         -relief raised \
         -borderwidth 1] \
      -row 0 \
      -column 0 \
      -sticky nsew
   grid \
      [frame .grepwidget.results \
         -relief raised \
         -borderwidth 1] \
      -row 1 \
      -column 0 \
      -sticky nsew
   grid \
      [frame .grepwidget.status \
         -relief raised \
         -borderwidth 1] \
      -row 2 \
      -column 0 \
      -sticky nsew
   grid \
      [frame .grepwidget.buttons \
         -relief raised \
         -borderwidth 1] \
      -row 3 \
      -column 0 \
      -sticky nsew
   grid rowconfigure .grepwidget 0 -weight 0
   grid rowconfigure .grepwidget 1 -weight 1
   grid rowconfigure .grepwidget 2 -weight 0
   grid rowconfigure .grepwidget 3 -weight 0
   grid columnconfigure .grepwidget 0 -weight 1
   tk_optionMenu .grepwidget.inputs.pos greppos Noun Verb Adjective Adverb
   grid \
      [label .grepwidget.inputs.label \
         -text "Substring:"] \
      -row 0 \
      -column 0 \
      -sticky ew \
      -padx 2 \
      -pady 2
   grid \
      [entry .grepwidget.inputs.word \
         -textvariable grepstr \
         -width 15 \
         -foreground Black \
         -background White] \
      -row 0 \
      -column 1 \
      -sticky ew \
      -padx 2 \
      -pady 2
   focus .grepwidget.inputs.word
   grid \
      .grepwidget.inputs.pos \
      -row 0 \
      -column 2 \
      -sticky ew \
      -padx 2 \
      -pady 2
   grid rowconfigure .grepwidget.inputs 0 -weight 1
   grid columnconfigure .grepwidget.inputs 0 -weight 0
   grid columnconfigure .grepwidget.inputs 1 -weight 1
   grid columnconfigure .grepwidget.inputs 2 -weight 0
   pack \
      [frame .grepwidget.results.frame] \
      -side top \
      -fill both \
      -expand true \
      -padx 8 \
      -pady 8
   grid \
      [listbox .grepwidget.results.frame.list \
         -yscrollcommand ".grepwidget.results.frame.yscroll set" \
         -foreground Black \
         -background White \
         -highlightthickness 0] \
      -row 0 \
      -column 0 \
      -sticky nsew
   grid \
      [scrollbar .grepwidget.results.frame.yscroll \
         -command ".grepwidget.results.frame.list yview" \
         -width 12 \
         -highlightthickness 0] \
      -row 0 \
      -column 1 \
      -sticky nsew
   grid rowconfigure .grepwidget.results.frame 0 -weight 1
   grid columnconfigure .grepwidget.results.frame 0 -weight 1
   grid columnconfigure .grepwidget.results.frame 1 -weight 0
   pack \
      [label .grepwidget.status.label \
         -text ""] \
      -side top
   pack \
      [frame .grepwidget.buttons.frame] \
      -side top
   grid \
      [button .grepwidget.buttons.frame.search \
         -text "Search" \
         -command {
            if {[.grepwidget.buttons.frame.search cget -text] == "Search"} {
               if {[string length $grepstr] >= 3} {
                  .grepwidget.buttons.frame.search configure \
                     -text "Abort"
                  .grepwidget.status.label configure \
                     -text "Searching... (press escape to abort)"
                  bind .grepwidget <Escape> {
                     .grepwidget.buttons.frame.search flash
                     .grepwidget.buttons.frame.search invoke
                  }
                  .grepwidget.results.frame.list delete 0 end
                  update
                  set posnumber [lsearch {{} Noun Verb Adjective Adverb} \
                     $greppos]
                  set clockstart [clock seconds]
		  switch $g_greptype {
		      "substring" {
			  set buf [search [fixword $grepstr] $posnumber 30 0]
		      }
		      "ending" {
			  set buf [fixgrep [search [fixword $grepstr] $posnumber 30 0] [fixword $grepstr]]
		      }
		  }
                  set clockstop [clock seconds]
                  set buflines [split $buf \n]
                  foreach line $buflines {
                     .grepwidget.results.frame.list insert end $line
                  } 
                  .grepwidget.buttons.frame.search configure \
                     -text "Search"
                  .grepwidget.status.label configure -text ""
                  bind .grepwidget <Escape> {}
               } else {
                  bell
	          set line "Error: search string less than 3 characters\n"
		  .grepwidget.results.frame.list delete 0 end
	          .grepwidget.results.frame.list insert end $line
               }
            } else {
               bell
               abortsearch
            }
         }] \
      -row 0 \
      -column 0 \
      -sticky nsew \
      -padx 2 \
      -pady 2
   grid \
      [button .grepwidget.buttons.frame.save \
         -text "Save" \
         -command "savetext grep"] \
      -row 0 \
      -column 1 \
      -sticky nsew \
      -padx 2 \
      -pady 2

# Printing only works on Unix systems, so don't offer the option on
# other platforms.
   if {$tcl_platform(platform) == "unix"} {
      grid \
         [button .grepwidget.buttons.frame.print \
            -text "Print" \
            -command "printtext grep"] \
         -row 0 \
         -column 2 \
         -sticky nsew \
         -padx 2 \
         -pady 2
   }
   grid \
      [button .grepwidget.buttons.frame.dismiss \
         -text "Dismiss" \
         -command "destroy .grepwidget"] \
      -row 0 \
      -column 3 \
      -sticky nsew \
      -padx 2 \
      -pady 2
   grid rowconfigure .grepwidget.buttons.frame 0 -weight 0
   grid columnconfigure .grepwidget.buttons.frame 0 -weight 1
   grid columnconfigure .grepwidget.buttons.frame 1 -weight 1
   grid columnconfigure .grepwidget.buttons.frame 2 -weight 1
   grid columnconfigure .grepwidget.buttons.frame 3 -weight 1
   bind .grepwidget.results.frame.list <ButtonRelease-1> {
      set chosengrep [.grepwidget.results.frame.list curselection]
      if {$chosengrep != {}} {
         set chosengrepword [.grepwidget.results.frame.list get \
            [lindex $chosengrep 0]]
         set g_searchword $chosengrepword
         set g_senses {}
         golookup
      }
   }
   bind .grepwidget <Return> {
      .grepwidget.buttons.frame.search flash
      .grepwidget.buttons.frame.search invoke
   }
}

### Utility functions

proc setwordwrap {wrap} {
   if {$wrap} {	
     .results.text configure -wrap word
     grid forget .results.scrollx
   } else {
     .results.text configure -wrap none
     grid .results.scrollx -row 1 -column 0 -sticky nsew
   }
}

proc golookup {} {
   global g_searchword
   global g_senses

   if {[fixword $g_searchword] == ""} return
   .results.text configure \
      -state normal
   .results.text delete 1.0 end
   .results.text configure \
      -state disabled
   set g_senses {}
   .posmenubar.label configure \
      -text "Searches for $g_searchword:"
   if {[displayvalidsearchmenus $g_searchword]} {
      displayoverview $g_searchword
      history_add $g_searchword {} 0 0
   }
}

proc fixword {word} {
   regsub "^ *" $word "" word ; # remove leading spaces
   regsub " *$" $word "" word ; # remove trailing spaces
   regsub -all " " $word "_" word  ; # change other spaces to underscores
   return $word 
}

proc fixgrep {grepbuf word} {
    set greplines [split $grepbuf "\n"]
    foreach line $greplines {
	if { ([regexp -nocase "$word\$" $line]) && !([regexp " " $line]) } {
	    append retbuf $line
	    append retbuf "\n"
	}
    }
    return $retbuf
}

proc updatefonts {} {
   global fontsizelist
   global fontname
   global fontsize
   .results.text configure \
      -font "-adobe-$fontname-medium-r-*-*-*-[lindex \
         $fontsizelist $fontsize]-*-*-*-*-*-*"
   .results.text tag configure helpstyle \
      -font "-adobe-$fontname-medium-r-*-*-*-[lindex $fontsizelist \
         [expr $fontsize - 1]]-*-*-*-*-*-*" \
      -foreground Blue \
      -lmargin1 20
   .results.text tag configure overviewhighlight \
      -font "-adobe-$fontname-bold-r-*-*-*-[lindex $fontsizelist \
         $fontsize]-*-*-*-*-*-*" \
      -foreground Red
}

proc clearall {} {
   global g_searchword
   global g_senses
   set g_searchword {}
   set g_senses {}
   .results.text configure \
      -state normal
   .results.text delete 1.0 end
   .results.text configure \
      -state disabled
   foreach pos {noun verb adj adv} { pack forget .posmenubar.$pos }
   pack forget .wordframe.overview
   .statusbar.status configure \
      -text "Enter search word and press return."
   .posmenubar.label configure \
      -text ""
}

proc generaterange {low high} {
   for {set i $low} {$i <= $high} {incr i} {lappend res $i}
   return $res
}

proc getsenselist {senses} {
   regsub -all {[^0-9*]+} $senses " " senses
   regsub "^ *" $senses "" senses
   regsub "\ +$" $senses "" senses
   if {[regexp {\*} $senses] || ($senses == {})} { set senses 0 }
   return $senses
}

proc blackout {q} {
   set objectlist {
      .menubar.file
      .menubar.history
      .menubar.options
      .menubar.help
      .wordframe.entry
      .wordframe.overview
      .posmenubar.noun
      .posmenubar.verb
      .posmenubar.adj
      .posmenubar.adv
      .posmenubar.entry
   }
   if {$q} {
      bind . <KeyPress-Escape> {bell; abortsearch}
      bind .wordframe.entry <Return> {}
      bind .results.text <Shift-Button-1> {}
      bind .results.text <Button-2> {}
      bind . <Control-s> {}
      foreach object $objectlist {
         $object configure \
            -state disabled
      }
   } else {
      bind . <KeyPress-Escape> {}
      bind .wordframe.entry <Return> golookup
      bind .results.text <Shift-Button-1> {shiftclickhandler %x %y}
      bind .results.text <Button-2> {shiftclickhandler %x %y}
      bind . <Control-s> controlshandler
      foreach object $objectlist {
         $object configure \
            -state normal
      }
   }
}

proc shiftclickhandler {x y} { 
   global g_searchword
   set newsearchword [.results.text get "@$x,$y wordstart" "@$x,$y wordend"]
   if {$newsearchword != "\n"} {
      set g_searchword $newsearchword
      golookup
   }
}

proc controlshandler {} {
   global g_searchword
   if {[catch {selection get} newsearchword]} {
      bell
      return
   }
   if {$newsearchword != ""} {
      set g_searchword $newsearchword
      golookup
   }
}


### Visual Components

grid \
   [frame .menubar \
      -relief raised \
      -borderwidth 1] \
   -row 0 \
   -column 0 \
   -sticky ew
grid \
   [frame .wordframe \
      -relief raised \
      -borderwidth 1] \
   -row 1 \
   -column 0 \
   -sticky ew
grid \
   [frame .posmenubar \
      -relief raised \
      -borderwidth 1] \
   -row 2 \
   -column 0 \
   -sticky ew
grid \
   [frame .results \
      -relief raised \
      -borderwidth 1] \
   -row 3 \
   -column 0 \
   -sticky nsew \
   -ipadx 3 \
   -ipady 3
grid \
   [frame .statusbar \
      -relief raised \
      -borderwidth 1] \
   -row 4 \
   -column 0 \
   -sticky ew
grid rowconfigure . 0 -weight 0
grid rowconfigure . 1 -weight 0
grid rowconfigure . 2 -weight 0
grid rowconfigure . 3 -weight 1
grid rowconfigure . 4 -weight 0
grid columnconfigure . 0 -weight 1
pack \
   [menubutton .menubar.file \
      -text "File" \
      -menu .menubar.file.menu \
      -relief flat] \
   [menubutton .menubar.history \
      -text "History" \
      -menu .menubar.history.menu \
      -relief flat] \
   [menubutton .menubar.options \
      -text "Options" \
      -menu .menubar.options.menu \
      -relief flat] \
   [menubutton .menubar.help \
      -text "Help" \
      -menu .menubar.help.menu \
      -relief flat] \
   -side left
pack \
   [label .wordframe.label \
      -text "Search Word:"] \
   [entry .wordframe.entry \
      -textvariable g_searchword \
      -width 40 \
      -background White \
      -foreground Black] \
   -side left \
   -padx 2 \
   -pady 2
focus .wordframe.entry
button .wordframe.overview \
   -text "Redisplay Overview" \
   -padx 3 \
   -pady 1 \
   -highlightthickness 0 \
   -relief raised \
   -command {
      set g_senses {}
      displayoverview $g_searchword
      history_add $g_searchword {} 0 0
   }
pack \
   [label .posmenubar.label \
      -text ""] \
   -side left \
   -padx 2 \
   -pady 2
menubutton .posmenubar.noun \
   -text "Noun" \
   -menu .posmenubar.noun.menu \
   -relief raised
menubutton .posmenubar.verb \
   -text "Verb" \
   -menu .posmenubar.verb.menu \
   -relief raised
menubutton .posmenubar.adj \
   -text "Adjective" \
   -menu .posmenubar.adj.menu \
   -relief raised
menubutton .posmenubar.adv \
   -text "Adverb" \
   -menu .posmenubar.adv.menu \
   -relief raised
pack \
   [entry .posmenubar.entry \
      -textvariable g_senses \
      -width 10 \
      -background White \
      -foreground Black] \
   [label .posmenubar.senselabel \
      -text "Senses:"] \
   -side right \
   -padx 2 \
   -pady 2
grid \
   [text .results.text \
      -wrap word \
      -relief sunken \
      -borderwidth 2 \
      -state disabled \
      -yscrollcommand ".results.scrolly set" \
      -xscrollcommand ".results.scrollx set" \
      -background White \
      -foreground Black \
      -width 80 \
      -height 25 \
      -highlightthickness 0] \
   -row 0 \
   -column 0 \
   -sticky nsew
updatefonts
grid \
   [scrollbar .results.scrolly \
      -command ".results.text yview" \
      -width 12 \
      -relief sunken \
      -borderwidth 2 \
      -highlightthickness 0] \
   -row 0 \
   -column 1 \
   -sticky nsew
grid \
   [scrollbar .results.scrollx \
      -orient horizontal \
      -command ".results.text xview" \
      -width 12 \
      -relief sunken \
      -borderwidth 2 \
      -highlightthickness 0] \
   -row 1 \
   -column 0 \
   -sticky nsew
if {$wordwrap} {grid forget .results.scrollx}
grid rowconfigure .results 0 -weight 1
grid rowconfigure .results 1 -weight 0
grid columnconfigure .results 0 -weight 1
grid columnconfigure .results 1 -weight 0
pack \
   [label .statusbar.status \
      -text "Enter search word and press return."] \
   -side left \
   -padx 2

### Regular Menus

if {$labonly} {
menu .menubar.file.menu \
   -tearoff false
menu .menubar.history.menu \
   -tearoff false
menu .menubar.options.menu \
   -tearoff false
menu .menubar.options.menu.font \
   -tearoff false
menu .menubar.help.menu \
   -tearoff false
.menubar.file.menu add command \
   -label "Find keywords by substring" \
   -command "grepword substring"
.menubar.file.menu add command \
   -label "Find keywords by ending" \
   -command "grepword ending"
.menubar.file.menu add separator
.menubar.file.menu add command \
   -label "Save current display (specify path)" \
   -command "savetext main"
} else {
menu .menubar.file.menu \
   -tearoff false
menu .menubar.history.menu \
   -tearoff false
menu .menubar.options.menu \
   -tearoff false
menu .menubar.options.menu.font \
   -tearoff false
menu .menubar.help.menu \
   -tearoff false
.menubar.file.menu add command \
   -label "Find keywords by substring" \
   -command "grepword substring"
.menubar.file.menu add separator
.menubar.file.menu add command \
   -label "Save current display (specify path)" \
   -command "savetext main"
}

# Printing only works on Unix systems, so don't offer the option on
# other platforms.
if {$tcl_platform(platform) == "unix"} {
   .menubar.file.menu add command \
      -label "Print current display" \
      -command "printtext main"
}
.menubar.file.menu add command \
   -label "Clear current display" \
   -command clearall
.menubar.file.menu add separator
if {$labonly} {
   .menubar.file.menu add command \
      -label "Reopen database" \
      -command reopendb
   .menubar.file.menu add separator
}
.menubar.file.menu add command \
   -label "Exit" \
   -command { destroy . }
.menubar.options.menu add checkbutton \
   -label "Show help with each search" \
   -variable showcontextualhelp
.menubar.options.menu add checkbutton \
   -label "Show descriptive gloss" \
   -variable showglosses \
   -command { glosses $showglosses }
.menubar.options.menu add checkbutton \
   -label "Wrap lines" \
   -variable wordwrap \
   -command {.menubar.history.menu invoke 0} 
.menubar.options.menu add separator
.menubar.options.menu add command \
   -label "Set advanced search options..." \
   -command {
      toplevel .adviewopt
      wm title .adviewopt "Advanced search options"
      wm transient .adviewopt .
      scan [wm geometry .] "%dx%d+%d+%d" geom_h geom_w geom_x geom_y
      wm geometry .adviewopt +[expr $geom_x+50]+[expr $geom_y+50]
      wm resizable .adviewopt 0 0
      grab set .adviewopt
      pack \
         [frame .adviewopt.fileinfo \
            -relief raised \
            -borderwidth 1] \
         [frame .adviewopt.byteoffset \
            -relief raised \
            -borderwidth 1] \
         [frame .adviewopt.senseflag \
            -relief raised \
            -borderwidth 1] \
         [frame .adviewopt.bottom \
            -relief raised \
            -borderwidth 1] \
         -side top \
         -fill both \
         -expand true
      pack \
         [frame .adviewopt.fileinfo.f \
            -relief flat \
            -borderwidth 10] \
         -side top \
         -fill both \
         -expand true
      pack \
         [label .adviewopt.fileinfo.f.label \
            -text "Lexical file information"] \
         [radiobutton .adviewopt.fileinfo.f.opt0 \
            -text "Don't show" \
            -variable showfileinfo \
            -value 0] \
         [radiobutton .adviewopt.fileinfo.f.opt1 \
            -text "Show with searches" \
            -variable showfileinfo \
            -value 1] \
         [radiobutton .adviewopt.fileinfo.f.opt2 \
            -text "Show with searches and overview" \
            -variable showfileinfo \
            -value 2] \
         -side top \
         -anchor w
      pack \
         [frame .adviewopt.byteoffset.f \
            -relief flat \
            -borderwidth 10] \
         -side top \
         -fill both \
         -expand true
      pack \
         [label .adviewopt.byteoffset.f.label \
            -text "Synset location in database file"] \
         [radiobutton .adviewopt.byteoffset.f.opt0 \
            -text "Don't show" \
            -variable showbyteoffset \
            -value 0] \
         [radiobutton .adviewopt.byteoffset.f.opt1 \
            -text "Show with searches" \
            -variable showbyteoffset \
            -value 1] \
         [radiobutton .adviewopt.byteoffset.f.opt2 \
            -text "Show with searches and overview" \
            -variable showbyteoffset \
            -value 2] \
         -side top \
         -anchor w
      pack \
         [frame .adviewopt.senseflag.f \
            -relief flat \
            -borderwidth 10] \
         -side top \
         -fill both \
         -expand true
      pack \
         [label .adviewopt.senseflag.f.label \
            -text "Sense number"] \
         [radiobutton .adviewopt.senseflag.f.opt0 \
            -text "Don't show" \
            -variable showsenseflag \
            -value 0] \
         [radiobutton .adviewopt.senseflag.f.opt1 \
            -text "Show with searches" \
            -variable showsenseflag \
            -value 1] \
         [radiobutton .adviewopt.senseflag.f.opt2 \
            -text "Show with searches and overview" \
            -variable showsenseflag \
            -value 2] \
         -side top \
         -anchor w
      pack \
         [button .adviewopt.bottom.ok \
            -text "Ok" \
            -command "destroy .adviewopt"] \
         -side top \
         -pady 5
   }   
.menubar.options.menu add command \
   -label "Set maximum history length..." \
   -command setmaxhistorylength
.menubar.options.menu add command \
   -label "Set font..." \
   -command {
      toplevel .fontopt
      wm title .fontopt "Font"
      wm transient .fontopt .
      scan [wm geometry .] "%dx%d+%d+%d" geom_h geom_w geom_x geom_y
      wm geometry .fontopt +[expr $geom_x+50]+[expr $geom_y+50]
      wm resizable .fontopt 0 0
      grab set .fontopt
      pack \
         [frame .fontopt.top] \
         [frame .fontopt.bottom \
            -relief raised \
            -borderwidth 1] \
         -side top \
         -fill both \
         -expand true
      pack \
         [frame .fontopt.top.face \
            -relief raised \
            -borderwidth 1] \
         [frame .fontopt.top.size \
            -relief raised \
            -borderwidth 1] \
         -side left \
         -fill both \
         -expand true
      pack \
         [frame .fontopt.top.face.f \
            -relief flat \
            -borderwidth 10] \
         -side top \
         -fill both \
         -expand true
      pack \
         [label .fontopt.top.face.f.label \
            -text "Typeface"] \
         [radiobutton .fontopt.top.face.f.courier \
            -text "Courier" \
            -variable fontname \
            -value courier] \
         [radiobutton .fontopt.top.face.f.helvetica \
            -text "Helvetica" \
            -variable fontname \
            -value helvetica] \
         [radiobutton .fontopt.top.face.f.times \
            -text "Times" \
            -variable fontname \
            -value times] \
         -side top \
         -anchor w
      pack \
         [frame .fontopt.top.size.f \
            -relief flat \
            -borderwidth 10] \
         -side top \
         -fill both \
         -expand true
      pack \
         [label .fontopt.top.size.f.label \
            -text "Size"] \
         [radiobutton .fontopt.top.size.f.small \
            -text "Small" \
            -variable fontsize \
            -value 1] \
         [radiobutton .fontopt.top.size.f.medium \
            -text "Medium" \
            -variable fontsize \
            -value 2] \
         [radiobutton .fontopt.top.size.f.large \
            -text "Large" \
            -variable fontsize \
            -value 3] \
         -side top \
         -anchor w
      pack \
         [button .fontopt.bottom.ok \
            -text "Ok" \
            -command {
               updatefonts
               destroy .fontopt
	       .menubar.history.menu invoke 0
            }] \
         -side top \
         -pady 5
   }
.menubar.options.menu add separator
.menubar.options.menu add command \
    -label "Save current options as default" \
    -command {saveoptions}
.menubar.help.menu add command \
   -label "Help on using the WordNet browser" \
   -command {showhelpwidget helpwidget_xwn $resourcedir/wnb.man "WordNet Browser Help"}
.menubar.help.menu add command \
   -label "Help on WordNet terminology" \
   -command {showhelpwidget helpwidget_wngloss $resourcedir/wngloss.man "WordNet Glossary"}
.menubar.help.menu add command \
   -label "Display the WordNet license" \
   -command {showhelpwidget helpwidget_license /usr/share/doc/wordnet/copyright "WordNet License"}
.menubar.help.menu add separator
.menubar.help.menu add command \
   -label "About the WordNet browser" \
   -command showaboutbox

### Dynamic menus (for each part of speech)
### The numbers here are from wnconsts.h in the Wordnet library.
### Unfortunately, Tcl doesn't recognise #defined constants.
### Please be sure to keep them synchronised.

menu .posmenubar.noun.menu \
   -tearoff false
menu .posmenubar.verb.menu \
   -tearoff false
menu .posmenubar.adj.menu \
   -tearoff false
menu .posmenubar.adv.menu \
   -tearoff false
set posmenu(1) { 
   { "Synonyms, ordered by estimated frequency" 2 {} }
   { "Synonyms, grouped by similarity" 27 {} }
   { "Antonyms" 1 {} }
   { "Coordinate Terms" 26 {} }
   { "Hypernyms (this is a kind of...)" -2 {} }
   { "Hyponyms (...is a kind of this), brief" 3 {} }
   { "Hyponyms (...is a kind of this), full" -3 {} }
   { "Holonyms (this is a part of...), regular" 13 {} }
   { "Holonyms (this is a part of...), inherited" -29 {} }
   { "Meronyms (parts of this), regular" 12 {} }
   { "Meronyms (parts of this), inherited" -28 {} }
   { "Derivationally related forms" 20 {} }
   { "Attributes (...is a value of this)" 18 {} }
   { "Domain" 21 {} }
   { "Domain Terms" 22 {} }
   { "Familiarity" 24 {} }
}
set posmenu(2) {
   { "Synonyms, ordered by estimated frequency" 2 {} }
   { "Synonyms, grouped by similarity" 27 {} }
   { "Antonyms" 1 {} }
   { "Coordinate Terms" 26 {} }
   { "Hypernyms (this is one way to...)" -2 {} }
   { "Troponyms (particular ways to...), brief" 3 {} }
   { "Troponyms (particular ways to...), full" -3 {} }
   { "This entails doing..." 4 {} }
   { "This causes..." 14 {} }
   { "Derivationally related forms" 20 {} }
   { "Sentence frames" 25 {} }
   { "Domain" 21 {} }
   { "Domain Terms" 22 {} }
   { "Familiarity" 24 {} }
}
set posmenu(3) {
   { "Synonyms" 5 {} }
   { "Antonyms" 1 {} }
   { "Related Noun" 17 {} }
   { "This is a value of..." 18 {} }
   { "Derivationally related forms" 20 {} }
   { "Domain" 21 {} }
   { "Domain Terms" 22 {} }
   { "Familiarity" 24 {} }
}
set posmenu(4) {
   { "Synonyms" 23 {} }
   { "Antonyms" 1 {} }
   { "Base Adjective" 17 {} }
   { "Derivationally related forms" 20 {} }
   { "Domain" 21 {} }
   { "Domain Terms" 22 {} }
   { "Familiarity" 24 {} }
}

### Bindings

bind .wordframe.entry <Return> golookup
bind .results.text <Shift-Button-1> {shiftclickhandler %x %y}
bind .results.text <Button-2> {shiftclickhandler %x %y}
bind . <Control-s> controlshandler
bind . <Control-g> "grepword substring"

set g_searchword [lindex $argv 0]
golookup