[go: up one dir, main page]

File: tkman.tcl

package info (click to toggle)
tkman 2.2-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 908 kB
  • ctags: 292
  • sloc: tcl: 8,264; makefile: 250; sh: 145
file content (2717 lines) | stat: -rw-r--r-- 98,276 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
#
# Bird?  Plane?  TkMan!  (TkPerson?)
#
#   initial prototype written March 24-25, 1993
#
#   Copyright (c) 1994-2003  Thomas A. Phelps
#



set manx(startuptime) [clock seconds]
set manx(reading) 0; set manx(startreading) -1
wm withdraw .; update idletasks


# could check for existing TkMan, if found raise its window and kill self,
# but person may want to run on different OSes from same screen


#--------------------------------------------------
#
# Shortcuts
#
#--------------------------------------------------

proc manShortcuts {w cmd} {
	global man manx short

#	set me $manx(name$w)
 	set me $manx(typein$w)
#	if {![string match "<*" $me]} { append me ".$manx(cursect$w)" }
#puts "$manx(man$w) => $manx(name$w).$manx(cursect$w)"

	set modeok [lmatch {man txt texi} $manx(mode$w)]
	if {$cmd ne "init" && (!$modeok || $manx(man$w) eq "")} return

	set present [expr {[lsearch -exact $manx(shortcuts) $me]!=-1}]
	if {$cmd eq "add" && !$present} {lappend manx(shortcuts) $me; set short($me) [clock seconds]
	} elseif {$cmd eq "sub" && $present} {set manx(shortcuts) [lfilter $me $manx(shortcuts)]}

	# update menu
	set m .shortcuts
	$m delete 0 last
	set len [llength $manx(shortcuts)]
	# store list by time added, sort on demand
	set list $manx(shortcuts)
	if {$man(shortcuts-sort)} {set list [lsort $list]}
	if {$len} {
		foreach i $list {
#			if {![regexp {^[<|]} $i]} {
				#  usual man page
#				if {[lsearch $manx(mandot) $i]>-1} {set name $i} {set name [file rootname $i]}
#			} else {set name $i}
			$m add command -label $i -command "incr stat(man-shortcut); manShowMan [list $i] {} \$curwin"
		}
	}

	foreach win [lmatches ".man*" [winfo children .]] {manShortcutsStatus $win}
	manMenuFit $m
}


proc manShortcutsStatus {w} {
	global man manx
	global bmb

#	set me $manx(name$w)
 	set me $manx(typein$w)
#	if {![string match "<*" $me]} { append me ".$manx(cursect$w)" }

	set modeok [lmatch {man texi txt} $manx(mode$w)]
	set present [expr {[lsearch -exact $manx(shortcuts) $me]!=-1}]
	set b $w.stoggle
	if {$modeok} {
		if {$present} {set cmd "sub"; set text "-"
		} else {set cmd "add"; set text "+"
		}
		$b configure -text $text; set bmb($b) "incr stat(page-shortcut-$cmd); manShortcuts $w $cmd"
	} else {$b configure -text "x"; set bmb($b) ""}
}



# compensate for Tk's nonalphnum-bounded wordstart and wordend
# should have companions to word{start,end} that are bounded by whitespace, not nonalpha
# (in this case analysis too complex for an extended wordstart)

proc manHotSpot {cmd t xy} {
	global man manx curwin

	set manchars {[ a-z0-9_.~/$+()-:<]}
	set manx(hotman$t) ""

	# click again in hot spot without time considerations
#	if {!catch{$t index hot.first} && [$t compare hot.first <= $xy] && [$t compare $xy >= hot.last]} {
#		set cmd get
#	}

	# act on command
	if {$cmd eq "clear"} {
		$t tag remove hot 1.0 end
		return
	}


	scan [$t index $xy] "%d.%d" line char
	scan [$t index "$line.$char lineend"] "%d.%d" bozo lineend

	# else $cmd=="show"
	$t tag remove hot 1.0 end


	# command line options trump
# FLAT, RAISED, SUNKEN, GROOVE, SOLID, or RIDGE
	if {0 && $manx(mode$curwin) eq "man" && ([$t get [set cmdstart "$xy wordstart-1c"]] eq "-" || [$t get [set cmdstart "$xy wordstart"]] eq "-")} {
		set skipe "\[]\[\t .,;:\{\}?\]"
		scan [$t index $cmdstart] "%d.%d" line char
		for {set c0 $char} {$c0>0 && ![regexp $skipe [$t get $line.$c0]]} {incr c0 -1} {}
		incr c0;	# went one too far
		scan [$t index "$xy wordend"] "%d.%d" line char
		for {set cn $char} {$cn<$lineend && ![regexp $skipe [$t get $line.$cn]]} {incr cn} {}

		if {[lsearch [$t tag names $line.$c0] "cmd"]==-1} {set toggle "add"} else {set toggle "remove"}
		$t tag $toggle cmd $line.$c0 $line.$cn

		selection own $curwin.man

		return
	}


### 8.0: possible to take advantage of tcl_endOfWord?
	# special cases when clicking on parentheses
	set c [$t get $line.$char]
	if {$c eq "("} {
		if {$char>0 && [$t get $line.$char-1c]!=" "} {incr char -1} else {incr char}
	} elseif {$c eq ")"} {
		if {$char>0} {incr char -1}
	}


	set lparen 0; set rparen 0
	# single space between name and parenthesized volume ok (found mainly in apropos but some page too)
	set fspace 0

	# gobble characters forwards
	for {set cn $char} {$cn<=$lineend && [regexp -nocase $manchars [set c [$t get $line.$cn]]]} {incr cn} {
		if {$c eq "("} {
			if {!$lparen} {set lparen $cn} else break
		} elseif {$c eq ")"} {
			if {!$rparen} {set rparen $cn; incr cn}
			break
		} elseif {$c eq ":" && ([string is space [$t get $line.$cn+1c]] || [string is space [$t get $line.$cn-1c]])} {
			break
		} elseif {[string is space $c]} {
			if {!$lparen && !$fspace && $cn<$lineend && [$t get $line.$cn+1c] eq "("} {set fspace 1} else break
		}
	}
	incr cn -1

	# gobble characters backwards
	for {set c0 $char} {$c0>=0 && [regexp -nocase $manchars [set c [$t get $line.$c0]]]} {incr c0 -1} {
		if {$c eq "("} {
			if {!$lparen} {set lparen $c0} else break
		} elseif {$c eq ")"} {
			break
		} elseif {$c eq ":" && ([string is space [$t get $line.$c0+1c]] || [string is space [$t get $line.$c0-1c]])} {
			break
		} elseif {[string is space $c]} {
			if {$lparen==[expr {$c0+1}] && !$fspace} {set fspace 1} else break
		}
	}
	incr c0

	# adjust if parentheses unbalanced
	if {!$lparen^!$rparen} {
		if {$lparen} {
			if {$char>$lparen} {set c0 [expr {$lparen+1}]} else {set cn [expr {$lparen-1}]}
			set lparen 0
		} else {
			# trim off right paren
			incr cn -1
		}

	# check for valid section number
	} elseif {$lparen && [lsearch $man(manList) [$t get $line.$lparen+1c]]==-1} {
		if {$char>$lparen} {set c0 [expr {$lparen+1}]} else {set cn [expr {$lparen-1}]}
		set lparen 0

	# nothing between parentheses
	} elseif {$lparen==[expr {$rparen-1}] && $lparen>0} {
		set cn [expr {$lparen-1}]

	# if have good parens, can't start with a "/"
	} elseif {$lparen && $rparen && [$t get $line.$c0] eq "/"} {
		incr c0
	} 

	# clean up sides
	while {$c0>0 && [lsearch {" "} [$t get $line.$c0]]>=0} {incr c0}
	while {$cn>$c0 && [lsearch {. - , " "} [$t get $line.$cn]]>=0} {incr cn -1}


	$t tag add hot $line.$c0 $line.$cn+1c
	set hyper [string trim [$t get $line.$c0 $line.$cn+1c]]
	# if clicked on absolute name, it's probably not a man page -- it is if I generated it
	#if [string match "/*" $hyper] {set hyper "<$hyper"}

	set manx(hotman$t) $hyper
}


proc manInstantiate {} {
	global stat manx

	incr stat(instantiation)
	incr manx(uid)
	incr manx(outcnt)
	set w [TkMan]
	manOutput

	return $w
}


proc manOutput {} {
	global manx

	set wins [lmatches ".man*" [winfo children .]]

	# make list of output choices
	set titleList {}
	foreach i [lsort -dictionary $wins] {
		set title "#[string range $i 4 end]"
		# special case for main window
		if {$title eq "#"} {append title 1}
		lappend titleList $title $i
	}

	# update list on each instantiation
	set wincnt 0
	foreach w $wins {
		set mb $w.output; set m $mb.m
		$m delete 0 last
		set mcnt 0
		foreach {label val} $titleList {
			set newlabel [expr {$wincnt==$mcnt?"Output":"=>$label"}]
			$m add radiobutton -label $label -variable manx(out$w) -value $val \
				-command "$mb configure -text $newlabel"
			incr mcnt
		}
		manMenuFit $m
		# if pointing to a zapped instance, reset to self
		if {![winfo exists $manx(out$w)]} {set manx(out$w) $w; $w.output configure -text "Output"}

		incr wincnt
	}

	# if multiple choices, show menu
	if {$manx(outcnt)==1} {
		pack forget .man.output
	} else {
		foreach w $wins {pack $w.output -before $w.occ -padx 2 -side left -expand yes}
	}
}


#--------------------------------------------------
#
# file completion
#
#--------------------------------------------------

proc manFilecomplete {w} {
	global manx

	set t $w.show; set wi $w.info
	set line $manx(typein$w)
	set file [string trim [llast $line] "<"]
	set posn [string last $file $line]

	set ll [llength [set fc [filecomplete $file]]]
#puts stdout "$line, $file, $posn, $fc"

	# check for no matches or more than one match
	if {!$ll} {
		manWinstderr $w "no matches"
		return
	} elseif {$ll>=2} {
		set matches {}; foreach i $fc {lappend matches [file tail $i]}
		if {[string length $matches]<50} {
			manWinstderr $w [lrest $matches]
		} else {
			manTextOpen $w
			$t insert end [lrest $matches]
			manTextClose $w
		}
		set fc [lfirst $fc]
	}

	# show new file name
	set manx(typein$w) [string range $line 0 [expr {$posn-1}]]$fc

	$w.mantypein icursor end
	$w.mantypein xview moveto 1
}


#--------------------------------------------------
#
# manNewMode -- collect mode change inits in single place
#	 types of modes: section, man, txt, apropos, glimpse, help
#
#--------------------------------------------------

proc manNewMode {w mode {n {""}}} {
	global man manx stat item2posn

	catch {unset item2posn}
	$w.c delete all
	set manx(tryoutline$w) 0

	set t $w.show
	$t tag add area 1.0; $t tag configure area -elide 0

	# save old highlights --> done continuously
#	manHighlights $w
	$w.high configure -foreground $man(buttfg) -background $man(buttbg) -font gui

	# save old yview
#	if {[$t index @0,0]==1.0 || [catch {if {[$t index @0,0]!=1.0} {set manx(yview,$manx(hv$w)) [$t index $manx(cursect$t)]}}]} {set manx(yview,$manx(hv$w)) [$t index @0,0]}

	set y [$t index @0,0]
	if {[$t tag ranges lastposn]!=""} {
		set y [$t index lastposn.first]
	} elseif {[$t tag ranges spot]!="" && [$t bbox spot.first]!=""} {
		set y [$t index spot.first]
	} elseif {[info exists manx(cursect$t)] && [set csy area$manx(cursect$t).first]!="" && [$t tag ranges $csy]!="" && [$t compare $csy > $y]} {
		set y $csy
	}
	# set to first nonblank line
	while {[$t compare $y < end] && [$t get $y] eq "\n"} {append y "+1l"}
#puts "save y for $manx(hv$w)"
	set manx(yview,$manx(hv$w)) [manPosn2OutnOff $t $y]

	set manx(oldmode$w) $manx(mode$w)
	set manx(mode$w) $mode
	set manx(manfull$w) ""
	set manx(catfull$w) ""
	set manx(man$w) ""
	set manx(name$w) ""
	set manx(num$w) ""
	set manx(tryoutline$w) 0

	# don't have to explicitly remove tags: done when delete all text "underneath"

	# reset searching
#	set manx(search,string$w) ""
	set manx(search,oldstring$w) ""
	manLineCnt $w ""
	searchboxKeyNav "" Escape 0 $t $w.info 0
#	if {$mode eq "section"} {searchboxKeyNav C s 0 $t "" 1}
	after 5 selection clear $t

#	set sbx(lastkeys$w) ""
	set manx(vect) 1
	set manx(try) 0
	set m $w.sections.m; eval destroy [winfo children $m]; $m delete 0 last
	# removing tag doesn't fire a Leave event
	$t configure -cursor left_ptr


	# disable various ui buttons for different types
	#   (paths, volumes, shortcuts, history always available)
	#
	#               man help txt info texi section apropos glimpse
	# yview          x   x    x   x    x     ?
	# sections       x   x        x    x
	# highlights     x   x    x        x
	# shortcuts +/-  x        x        x
	# print          x

	set high(0) disabled; set high(1) normal

	set h $high([lmatch {man help info texi rfc} $mode]); $w.sections configure -state $h
	set h $high([lmatch {man txt texi rfc help} $mode])
		# $w.high state controlled by highlighting code
		foreach i {high} {$w.$i configure -state $h}
#	if {![lmatch {man txt help} $mode]} {
##		foreach i {high hsub} {$w.$i configure -state disabled}
#	}
#	if {![lmatch {man txt rfc texi} $mode]} {
#		foreach i {stoggle} {$w.$i configure -state disabled}
#	}
	if {$man(print)!=""} {set h $high([lmatch man $mode]); .occ entryconfigure "Kill Trees" -state $h}

	manHyper $w $n
}


# returns outlinesect/lineoff.charoff
proc manPosn2OutnOff {t posn} {
	global manx

	if {[catch {scan [$t index $posn] "%d.%d" line char}]} return
	for {set s $posn+2c} {$s!="" && ![string match "js*" $s]} {set s [$t mark previous $s]} {}
	if {$s eq ""} {return "1/0.0"}

	scan [$t index $s] "%d" basestartline
	regexp {js(.*)} $s all basenum

	return "$basenum/[expr {$line-$basestartline}].$char"
#puts "*** $first => $baserel"
}


# set mouse clicks for hyperlinking
proc manHyper {w {n ""}} {
	global man manx

	set t $w.show

	# clear old settings
	foreach i {"Double-" ""} {
		foreach j {"" "Alt-" "Shift-"} {
			$t tag bind hyper <${j}${i}ButtonRelease-1> "format nothing"
		}
	}

	if {$man(hyperclick) eq "double"} {set mod "Double-"} else {set mod ""}
	$t tag bind hyper <ButtonRelease-1> "
		if {\$manx(hotman$t)!={}} { set manx(typein$w) \$manx(hotman$t) }
	"
	$t tag bind hyper <${mod}ButtonRelease-1> "+
		if {\$manx(hotman$t)!={}} { incr stat(man-hyper); manShowMan \$manx(hotman$t) $n \$manx(out$w) }
	"
	$t tag bind hyper <Shift-${mod}ButtonRelease-1> "
		if {\$manx(hotman$t)!={}} { incr stat(man-hyper); set manx(shift) 1; manShowMan \$manx(hotman$t) $n \$manx(out$w) }
	"
	$t tag bind hyper <Alt-${mod}ButtonRelease-1> "
		if {\$manx(hotman$t)!={}} { set manx(typein$w) \$manx(hotman$t); manShowMan <\$manx(hotman$t) \$manx(out$w) }
	"

	# (<Button-1> and <Button1-Motion> don't change)
}



#--------------------------------------------------
#
# manXXX - miscellaneous medium-level commands
#
#--------------------------------------------------

# just jab some text into the widget regardless of state
proc manTextPlug {t posn args} {
	set state [$t cget -state]; $t configure -state normal
	eval $t insert $posn $args
	$t configure -state $state
}

proc manTextOpen {w} {
	global man manx

	#raise $w
	cursorBusy
	set t $w.show
	$t configure -state normal
	$t delete 1.0 end
	# delete all marks and tags
	set manx(sectposns$w) ""
	set manx(cursect$t) js1
	foreach i [$t mark names] {if {$i!="insert"&&$i!="current"} {$t mark unset $i}}
	foreach i [$t tag names] {if {[regexp {(area)?js\d*} $i]} {$t tag delete $i}}
	$t mark set js1 1.0; $t mark gravity js1 left
}

proc manTextClose {w} {
	set t $w.show

	# zap final newline so last line is flush with bottom
	if {[$t get "end -2c"] eq "\n"} {$t delete "end -2c"}

	$t tag add hyper 1.0 end

	if {[lsearch [$t mark names] "endcontent"]==-1} {$t insert end " "; $t mark set endcontent end-1c}
	$t configure -state disabled
	cursorUnset
	$t mark set xmark 1.0
#	if {[lsearch [$t mark names] "endcontent"]==-1} {$t mark set endcontent end}
}

proc manResetEnv {} {
	global env man manx mani

	set manpath {}
	foreach i $manx(paths) {if {$man($i)} {append manpath :$i}}
	set env(MANPATH) [string range $manpath 1 end]

	# also updates contents of volume listings
	foreach i $manx(manList) {
		if {[lsearch $manx(specialvols) $i]==-1 || $i eq "all"} {set mani($i,form) ""}
	}
}

proc manSetSect {w n} {
	global manx mani

	set manx(cursect$w) *

	# passed full pathname, search for volume
	if {[regexp {^([A-Z]:)?/} $n]} {
		set dir [file dirname $n]
		# try to find (possibly remapped) volume
		foreach vol $mani(manList) {
			if {[lsearch -exact $mani($vol,dirs) $dir]!=-1} {
				set manx(cursect$w) $vol
#				set f [lsearch -exact $mani(manList) $vol]
				return $vol
			}
		}

		# else if has .<char>, take that
		if {[regexp {\.(.)[^/\.]*$} $n all letter]} {
			set manx(cursect$w) $letter
			return $letter
		}

	# passed alphanum, use if valid
	} elseif {[set f [lsearch $manx(manList) $n]]!=-1} {
		set manx(cursect$w) $n
	}
#		$w.volnow configure -text "([lindex $manx(manList) $f]) [lindex $manx(manTitleList) $f]"
#$w.volnow configure -text ""
	return $n
}

# put entire pulldown menu on screen, using progressively smaller fonts as necessary
proc manMenuFit {m} {
	global man manx

	if {[winfo class $m]!="Menu"} {puts stderr "$m not of Menu class"; exit 1}
	if {[$m index last] eq "none"} return

	set sh [winfo screenheight $m]

	# starting with current gui font size, pick smaller font as needed
	# until all entries fit on screen or out of smaller fonts
	set ok 0
	for {set i [expr {[lsearch $manx(sizes) $man(gui-points)]+1}]} {$i>=0} {incr i -1} {
		set p [lindex $manx(pts) $i]
		set f [spec2font $man(gui-family) $man(gui-style) $p]
		$m configure -font $f; update idletasks
		set mh [winfo reqheight $m]
#DEBUG {puts stderr "manMenuFit $m: $f, $mh, $sh"}
		if {$mh<$sh} {set ok 1; break}
	}

	# if menu still too big for screen, clip entries from bottom until it is
	if {!$ok} {
		set ctr 0
		# kind of heavyweight but don't need to do this often
		while {[winfo reqheight $m]>=$sh} {
			$m delete last; incr ctr; update idletasks
		}
		# make space for "too many" at bottom
		$m delete last; incr ctr
		$m add command -label "($ctr too many to show)" -state disabled
	}
}


#--------------------------------------------------
#
# manInit -- determine which sections exist,
#
#--------------------------------------------------

proc manInit {} {
	global man manx mani

	if {$manx(init)} return

	manReadSects
	# MANPATH may have shifted during startup
	manResetEnv
	set manx(init) 1
}



# max levels = 2 for man (section, subsection), 4 for Texinfo (chapter, section, subsection, subsubsection), help = 3 (h1, h2, h3)
set manx(MAXLEVELS) 4


# man page, Texinfo, Help
# THIS IS A BOTTLENECK
proc manShowManFoundSetText {w t key} {
	global man manx


	set alljs [lmatches "js*" [$t mark names]]
	if {[llength $alljs]==0} {set alljs 1.0}
	set manx(sectposns$w) [lsort -dictionary $alljs]
	set manx(nextposns$w) [concat [lrange $manx(sectposns$w) 1 end] endcontent]
	set manx(cursect$t) [lfirst $manx(sectposns$w)]


	# fix up sections: remove hyper tag and reformat short sections -- always
	# if short enough and no subsections, de-section-ify it.  don't see diffs, though
	# count lines in each section (not shown for short sections)
	set manx(sectname$t) {}
	# can speed up processing of Texinfo by processing in bulk, which is good because they have many header lines
	if {$manx(mode$w) eq "texi"} {
		if {$man(showsectmenu)} {set manx(sectname$t) [split [$t get [lfirst $manx(sectposns$w)] "[llast $manx(sectposns$w)] lineend"] "\n"]} else {set manx(sectname$t) ""}
		foreach now $manx(sectposns$w) {set manx(lcnt$now) [set manx(lcnt0$now) 0]}
		$t tag remove hyper 1.0 end
	} else {
		scan [$t index [lfirst $manx(sectposns$w)]] "%d" linenow
		foreach now $manx(sectposns$w) next $manx(nextposns$w) {
			lappend manx(sectname$t) [$t get $now "$now lineend"]
			scan [$t index $next] "%d" linenext
			set manx(lcnt$now) [set manx(lcnt0$now) [expr {$linenext-$linenow-1}]]
			set linenow $linenext
		}
		$t tag remove hyper $now "$now lineend"
	}

	set manx(sectlevels$t) {}
	$t configure -state normal
	foreach now $manx(sectposns$w) next $manx(nextposns$w) secttxt $manx(sectname$t) {
		lappend manx(sectlevels$t) [regsub -all {\.} $now {\.} junk]

		for {set sup $now} {[regexp $manx(supregexp) $sup all supnum]} {set sup "js$supnum"} {
			catch {incr manx(lcntjs$supnum) [expr {$manx(lcnt$now)+1}]}
		}

		set skip($now) 0

		# crunch short man sections, unless man(outline) off 
		if {$manx(mode$w) eq "man" && $man(outline) ne "off" && ($manx(lcnt$now)<=2 || ($manx(lcnt$now)<=5 && $secttxt ne "Synopsis" && $secttxt ne "Files")) && [lsearch -glob $manx(sectposns$w) $now.*]==-1} {
			set ss "$now linestart+1l"; set se "$now linestart+1l+$manx(lcnt$now)l"
			set fNAME [string equal $secttxt "Name"]; # normalized by RosettaMan
#			set fSYNOPSIS [string equal $secttxt "Synopsis"]; # only used once, not in loop

			# quick feasiblity check
			if {[$t compare "$ss+[expr {$manx(screencols)*2}]c" > $se]} {
			set utxt [$t get $ss $se]; regsub -all " +" $utxt "" txt; set txtlen [string length $txt]
#			if {$txtlen<60 || ($txtlen<80 && $fNAME)} {
#			if {$txtlen<$man(columns)-10 || ($txtlen<$man(columns)*1.1 && $fNAME)} {
			if {$txtlen<$manx(screencols) || ($txtlen<$manx(screencols)*1.1 && $fNAME)} {
				set skip($now) 1
				set manx(lcnt$now) [set manx(lcnt0$now) 1]

				# append lines onto header, replacing with \n
				# have to zap text in buffer rather than insert new text so as to preserve tags
				$t insert "$now lineend" "  "
#				$t insert "$now lineend" "  " {} $txt [expr {$fNAME?"sc":""}]
				for {scan [$t index $se-1l] "%d" i} {[$t compare $i.0 > $now]} {incr i -1} {
					while {[regexp "\[ \t\]" [$t get $i.0]]} {$t delete $i.0}
					# RosettaMan strips trailing spaces
					# make internals elided but keep \n's around for diff line numbers
#					if $fSYNOPSIS {$t insert $i.0 " //"}
					$t insert $i.0 " "; $t delete $i.0-1c; $t insert $i.0 "\n" elide
				}

				# strip out multiple spaces
				foreach {junk s} [$t tag nextrange h2 1.0] {}; append s "+3c"
				while {[set x [$t search "  " $s 2.0]]!=""} {$t delete $x}

#				set zapend [expr int([$t index $se])]
#				for {set i [expr int([$t index $ss])]} {$i<$zapend} {incr i} {$t delete $i.0 "$i.0 lineend"}

				# since accept more characters to put NAME on single line, make them small
				if {$fNAME} {$t tag add sc "$now linestart+7c" "$now lineend"}
			}
			}
		}
	}
	$t configure -state disabled



	set manx(tryoutline$w) [expr {$manx(mode$w) eq "texi" || ($man(outline) ne "off"
						  # exceptions: help page, full page fits on screen
						  && $manx(manfull$w) ne "_help" && [$t bbox endcontent] eq "")}]


	### after reading in text, build sections menu, count lines, show highlights
	# highlights
#	if {$manx(catfull$w)!=""} {set manx(hv$w) $manx(catfull$w)} {set manx(hv$w) $manx(manfull$w)}
	# always key highlights to name of source
	set manx(hv$w) $key


	if {$manx(tryoutline$w)} {
		for {set i 0} {$i<$manx(MAXLEVELS)} {incr i} {set last($i) ""}

		$t configure -state normal
		foreach now $manx(sectposns$w) level $manx(sectlevels$t) secttxt $manx(sectname$t) {
#if {![regexp {[[:lower:]]} $txt] && [string length $txt]>3} { set txt [string tolower $txt] } -- casified by RosettaMan

			# expand/collapse section and subsections by clicking on title
			if {!$skip($now)} {
				set closed 1; set imagename "closed"
				if {$manx(mode$w) eq "man" && ($man(outline) eq "allexp" || ([string match "allbut*" $man(outline)] && [regexp -nocase $man(outlinebut) $secttxt$manx(lcnt$now)]))} {set closed ""; set imagename "opened"}

				set tags [$t tag names "$now linestart"]
				$t image create "$now linestart" -image $imagename
				foreach tag $tags {$t tag add $tag "$now linestart"}

				# number of lines within each section (# subsections in Texinfo)
				if {$manx(lcnt$now)>0} {$t insert "$now lineend" "     $manx(lcnt$now)" sc}
				#if {$firstsect} {$t insert "$eol lineend" "  lines"; set firstsect 0}

				$t tag configure area$now -elide $closed
				$t tag add outline "$now linestart" "$now lineend"; # if +1l can click to area off the right
				# maybe indent heading only, not body
				set lmar [expr {0.3*(1+$level)}]c; $t tag configure area$now -lmargin1 $lmar -lmargin2 $lmar
			}

			# man page subsections always visible
			if {$level>0 && $manx(mode$w) eq "man"} {$t tag add alwaysvis "$now linestart" "$now lineend+1c"}

			# at start of new entity at that level, close up everything at that level and below (== higher #)
			for {set j $level} {$j<$manx(MAXLEVELS)} {incr j} {
				if {$last($j)!=""} {$t tag add area$last($j) "$last($j) lineend" "$now linestart"; set last($j) ""} else break
			}
			set last($level) $now
		}

		for {set j $level} {$j<$manx(MAXLEVELS)} {incr j} {
			if {$last($j)!=""} {$t tag add area$last($j) "$last($j) lineend" "endcontent"} else break
		}
		$t configure -state disabled
	}


	# adjust priorities after flurry of tag creation
	foreach tag [concat $manx(show-tags) hyper elide] {$t tag raise $tag}


#	manHighlights $w get --> man pages want to attach highlights after diffs so offsets in menu accurate
#	manYview $w	--> let Texinfo fault in corresponding section

	if {$man(showsectmenu)} {after 1000 manMakeSectMenuAfter $w $t}

	# crucial "after" to prevent whole system from locking up!
	# maybe works ok with Tcl 8.0 final; have to try that some time
	after 100 focus $t
#	focus $t
}

proc manMakeSectMenuAfter {w t} {
	global man manx

	# make sections menu
	set m [set mb $w.sections].m
	if {$manx(tryoutline$w)} {
# && [winfo manager $mb]
		$m add command -label "Expand next/scroll" -accelerator "Return" -command "manDownSmart $w $t"
# people can figure this out without help
		$m add command -label "Toggle current" -accelerator "Button 1 on header" -command "manOutline $t -1 \$manx(cursect$t)"
		$m add command -label "Current + hierarchy" -accelerator "Shift-Button 1 on header" -command "manOutline $t -1 \$manx(cursect$t) 1"
# believe me, you don't want this
#		if {$manx(mode$w) eq "man"} {$m add command -label "Expand All" -command "manOutline $t {} * 1; $t yview moveto 0"}
		# warning: on=0

		set manx(texishowsubs) ""
		if {$manx(mode$w) eq "texi"} {
			$m add checkbutton -label "Show All Subsection Heads" -variable manx(texishowsubs) -onvalue 0 -offvalue "" -command "foreach tag {section subsection subsubsection} {$t tag configure \$tag -elide \$manx(texishowsubs); $t tag raise \$tag}; $t yview moveto 0"
			if {[catch {$t index js2}] || [llength $manx(sectposns$w)]<40} {$w.sections.m invoke "last"}
		}
		$m add command -label "Collapse current" -accelerator "Button 3 anywhere" -command "manOutline $t 1 \$manx(cursect$t)"
		$m add command -label "Collapse all" -accelerator "Double-Button 3" -command "manOutline $t 1 *; $t yview moveto 0; if {\$manx(mode$w) eq \"man\"} {notemarks $w $t}"
		$m add separator
	}


	# compute number of levels to show in Sections (always build Sections in case put back in in medias res via Preferences)
	for {set i 0; set prefix ""} {$i<$manx(MAXLEVELS)} {incr i} {
		set clevel($i) 0
		set pfx($i) $prefix; append prefix "   "
	}

	foreach level $manx(sectlevels$t) {incr clevel($level)}

	set showlevel 0
	set scnt $clevel(0)
	for {set i 1} {$i<$manx(MAXLEVELS)} {incr i} {
		incr scnt $clevel($i)
		if {$scnt>50} break
		incr showlevel
	}

	set first 1
	foreach now $manx(sectposns$w) txt $manx(sectname$t) level $manx(sectlevels$t) {
		if {$level<=$showlevel} {
			$m add command -label "$pfx($level)$txt" -command "incr stat(page-section); manOutlineYview $t $now"
			# may have stuffed more text into top
			# (also have to special case 1.0 in manOutlineYview)
			if {$first} {$m entryconfigure last -command "incr stat(page-section); $t see 1.0"; set first 0}
		}
	}

#	if {![string match "_*" $key] && $man(headfoot)!=""} {
	if {$manx(mode$w) eq "man" && $man(headfoot) ne ""} {
		$m add separator
		$m add command -label "Header and Footer" -command "incr stat(page-section); $t yview endcontent"
#headfoot"
	}
	configurestate $mb
# [llength $ml]
#PROFILE "manMenuFit"
	after 500 manMenuFit $w.sections.m
#PROFILE "done manMenuFit" -- up to a quarter second for Emacs
}



proc manYview {w} {
	global manx man

	set t $w.show
	if {[catch {set y $manx(yview,$manx(hv$w))}]} {set y "1/1.0"
	} else {
		# with outlining, hard to determine last scroll position, so just highlight it
#$t tag remove lastposn 1.0 end; 
#puts -nonewline "\amanx(hv\$w)=$manx(hv$w).  nb $y => "
		regexp {(.*)/(.*)\.(.*)} $y all sect loff coff; set y "js$sect+${loff}l+${coff}c"
#puts $y
		if {[$t index $y]!=1.0} {
			if {$manx(tryoutline$w)} {nb $t lastposn $y $y} else {manOutlineYview $t $y}
		}
#		unset manx(yview,$manx(hv$w))
	}
	$t mark set xmark [$t index @0,0]
}


proc manOutlineYview {t line} {
	global man

	if {[catch {set line [$t index $line]}] || [$t compare $line < 2.0]} return; # don't open top section when first show page
	manOutline $t "" $line
	$t yview $line-$man(high,vcontext)l
	$t see $line
}

proc manOutlineSect {t sect} {
	global manx curwin

	if {[catch {set now [$t index $sect]}]} {return 0.0}
	regexp {(.*)/(.*)} [manPosn2OutnOff $t $sect] all new junk
#	set new [lfirst $manx(sectposns$curwin)]
#	foreach try $manx(sectposns$curwin) {
#		if {[$t compare "$try linestart" > $now]} break
#		set new $try
#	}
	return "js$new"
}



# smart scrolling: try to expand sections rather than scrolling
# if unexpanded section on sixth line or later, expand
proc manDownSmart {w t} {
	global man manx stat

	if {!$manx(tryoutline$w) || [lsearch {section apropos glimpse info} $manx(mode$w)]>-1 || [catch {$t index js1}]} {$t yview scroll 1 pages; return}
	incr stat(page-downsmart)

	set sect $manx(cursect$t)
	if {[$t bbox $sect] eq ""} {
		set sect [manOutlineSect $t @0,0]
		if {[$t bbox $sect] eq ""} {
			set sect [lindex $manx(sectposns$w) [expr {1+[lsearch $manx(sectposns$w) $sect]}]]
		}
	}

	set x [lsearch $manx(sectposns$w) $sect]
	while 1 {
		set openme [lindex $manx(sectposns$w) $x]
		if {$openme eq "" || [$t bbox $openme] eq ""} {
			$t yview scroll 1 pages
			break

		} elseif {[$t tag cget area$openme -elide]=="1"} {
			# optionally collapse previous at same level: 
			# high level structural context more important than last few lines of previous section

			if {$man(tidyout)} {
				set level [regsub -all {\.} $openme {\.} junk]; set curlevel 1000
				for {set i [expr {$x-1}]} {$i>=0} {incr i -1} {
					set cursect [lindex $manx(sectposns$w) $i]
					set curlevel [regsub -all {\.} $cursect {\.} junk]
					if {$curlevel<$level} {$t see $cursect; break}
					manOutline2 $t 1 $cursect
					#if {$curlevel==$level} break
				}
			}

			manOutline $t 0 [set manx(cursect$t) $openme]
			$t tag remove spot 1.0 end; $t tag add spot $openme

			break
		} else {incr x}
	}

	update idletasks
}



proc manOutline {t finv sect {prop 0}} {
	global manx curwin

	set time0 [clock clicks]

	if {!$manx(tryoutline$curwin) || [string trim $sect] eq ""} return

	# if explicitly playing with one section, don't subsequently open all of them
	set manx(hitlist$t) {}


	if {$sect eq "*"} {set sect $manx(sectposns$curwin)}

	if {[llength $sect]>1} {
		foreach s $sect {manOutline $t $finv $s $prop}
		return
	} elseif {![string match "js*" $sect]} {
		# if not passed an outline tag, search for nearest previous one
		if {[catch {set now [$t index $sect]}]} return
		set sect [manOutlineSect $t $sect]
	}

	set oldfinv [$t tag cget area$sect -elide]
	if {$finv==-1} {
		set finv [expr {$oldfinv==""?1:""}]
	} elseif {$finv==0} {set finv ""}

	# could be disconcerting, have to see how it wears
	if {$finv==""} { foreach tag $manx(show-ftags) {$t tag remove $tag 1.0 end} }

	set sects $sect
	if {$prop} {
		set su $sect
		# close up parents
		if {$finv=="1"} {
			while {[regexp $manx(supregexp) $su all sunum]} {lappend sects [set su "js$sunum"]}

		# open up all children
		} else {
			set lev [regsub -all {\.} $sect {\.} junk]
			foreach s [lrange $manx(sectposns$curwin) [expr {1+[lsearch $manx(sectposns$curwin) $sect]}] end] {
				if {[regsub -all {\.} $s {\.} junk] <= $lev} break
				lappend sects $s
			}
		}
	}
	foreach s $sects {manOutline2 $t $finv $s}

	# maintain context
	if {$finv!=1} {
		$t yview [set manx(cursect$t) $sect]
		if {[$t dlineinfo $sect-5l] eq ""} {$t yview scroll -5 units}
	}

# this is very fast now -- if $manx(mondostats) {manWinstdout $curwin "[lfirst [manWinstdout $curwin]]    time [expr ([clock clicks]-$time0)/1000000.0] sec"}
}


proc manOutline2 {t finv sect} {
	global man manx curwin texix$t stat
	upvar #0 texix$t texix

	# if showing, show parent too
	if {$finv!=1} {
		if {[regexp $manx(supregexp) $sect all num]} {manOutline2 $t $finv "js$num"}
	}
	if {[lsearch $manx(sectposns$curwin) $sect]==-1 || [lsearch -exact [$t tag names $sect] outline]==-1} return

	set oldfinv [$t tag cget area$sect -elide]
	if {$finv==$oldfinv} return

	$t tag configure area$sect -elide $finv
	set txtstate [$t cget -state]
	$t configure -state normal
	$t image configure "$sect linestart" -image [expr {$finv==1?"closed":"opened"}]
#$man(subsect-show)!="never"
#$man(subsect-show)
	$t configure -state $txtstate
	if {$finv==1} {incr stat(outline-collapse)} else {incr stat(outline-expand)}

	# generalize this
	# if Texinfo, may need to format (after expansion, otherwise have to search in nested elide)
	if {$finv!=1 && $manx(mode$curwin) eq "texi"} {
# && $texix($sect)!=""} {
		cursorBusy 0
		texiCallback $t $sect; # use anchored points to ease bookkeeping for texiMarkup
		cursorUnset
		scan [$t index end-1c] "%d" lastline 
		manLineCnt $curwin $lastline; # counts \n-terminated lines, sigh
	}
}


proc manLineCnt {w cnt {unit ""}} {
	global man manx

	if {$cnt==""} {$w.search.cnt configure -text ""; return}

	if {$unit==""} {
		if {$man(lengthchunk) eq ""} return;	# no report
		set unit [textmanip::plural $cnt $man(lengthchunk)]
		set cnt [expr {1+int($cnt/$manx($man(lengthchunk)-scale))}]
	}
	$w.search.cnt configure -text "$cnt $unit"
}


proc manManPipe {f} {
	global man manx

#	if {$man(gtar)!="" && [regexp "(.*\.tar($manx(zregexp)?))/(.+)" $f all tar z subf]} {
#		if {$z!=""} {set z "z"}
#		set pipe "$man(gtar) -xO${z}f $subf"
#		if {[regexp $manx(zregexp) $subf]} {append pipe " | $man(zcat)"}; # compress within tar
#
#	} else
	if {[regexp $manx(zregexp) $f] || [regexp $manx(zregexp) [file dirname $f]]} {
		set pipe $man(zcat)
	} else {set pipe "cat"}

	return "$pipe $f"
}

# (proper) man page and text file finishings
proc manShowManStatus {w} {
	global man manx pagecnt

	set t $w.show; set wi $w.info

	### update status information

	# show section
	manSetSect $w $manx(manfull$w)

	# typein field
	set manx(typein$w) $manx(name$w)
	$w.mantypein icursor end

	# history - save entire path, including any compression suffix
	set manx(history$w) \
		[lrange [setinsert $manx(history$w) 0 [list $manx(manfull$w)]] 0 [expr {$man(maxhistory)-1}]]
	# should do this as a postcommand, but that's too messy
	set m [set mb $w.history].m
	$m delete 0 last
	$m post 0 0; $m unpost; # needed to compensate for Tk menu bug
	foreach i $manx(history$w) {
		if {[llength [lfirst $i]]>=2} {set l [lfirst $i]} else {set l $i}
		if {![regexp {^[|<]} $l]} {set l [zapZ [file tail $l]]}
		$m add command -label $l -command "incr stat(man-history); manShowMan $i {} $w"
	}
	configurestate $mb
# [llength $manx(history$w)]
	manMenuFit $m


	# shortcuts
	manShortcutsStatus $w


	# line count
	scan [$t index end] "%d" linecnt 
	manLineCnt $w $linecnt

	# page count statistics
	set f [zapZ $manx(manfull$w)]
	if {![string match "<*" $f]} {set f [file tail $f]}
	if {![info exists pagecnt($f)]} {set pagecnt($f) [list 0 0 -1 0 [clock seconds]]}

	foreach {times lasttime cksum nlines firsttime} $pagecnt($f) break
	if {$firsttime==""} {set firsttime [clock seconds]}
	if {$cksum==""} {set cksum -1}
	set pagecnt($f) [list [expr {1+$times}] [clock seconds] $cksum $linecnt $firsttime]
}


proc zapZ! {f} {
	uplevel 1 set $f \[zapZ \[set $f\]\]
}
proc zapZ {f} {
	global manx
	return [expr {[regexp $manx(zregexp) $f]?[file rootname $f]:$f}]
}


#--------------------------------------------------
#
# manShowText -- use searching, etc. facilities for non-man page text
#	 to use via `send', pass full path name of text file to show
#
#--------------------------------------------------

proc manShowText {f0 {w .man} {keep 0}} {
	global man manx stat

	set wi $w.info

# maybe want to use `file' to check for ASCII files
#	if {![file readable $f] || [file isdirectory $f]} {
#		manWinstdout $w "Can't read $f"
#		return
#	}

	set t $w.show

	if {[string match <* $f0]} {
		set f [fileexp [string range $f0 1 end]]
		if {[string length $f]==0} return
		if {[regexp $manx(zregexp) $f]} {set f "|$man(zcat) $f"}
# {set f "|cat $f"}
	} else {set f [pipeexp $f0]}

	# strain out control characters and show nroff files
#	append f " | rman"

	manNewMode $w txt; incr stat(txt)

	manTextOpen $w
	if {[catch {
		set fid [open $f]
		$t insert end [read $fid]
		close $fid
	}]} {manTextClose $w; manWinstderr $w "Trouble reading $f0"; return}
	manTextClose $w
	if {!$keep} {pack forget $w.dups}

	if {[file isfile $f]} {cd [file dirname [glob $f]]}


	### update status information

	# could always save full path here, but that occupies too much screen real estate
	set manx(man$w) $f0
	set manx(num$w) X
	set manx(hv$w) [bolg [zapZ $f] ~]
	set manx(manfull$w) $f0
	set manx(catfull$w) $f
	set manx(name$w) $f0
	manShowManStatus $w

	manWinstdout $w $manx(manfull$w)
	manHighlights $w get; manYview $w
	focus $t

	return $f
}



#--------------------------------------------------
#
# manShowTexi -- use searching, etc. facilities for non-man page text
#	 to use via `send', pass full path name of text file to show
#
#--------------------------------------------------

proc manShowTexi {f0 {w .man} {keep 0}} {
	global man manx stat

	set t $w.show
	set wi $w.info

	set f $f0
#	if {[regexp $manx(zregexp) $f]} {set f "|$man(zcat) $f"}

	manNewMode $w texi; incr stat(texi)
#	manNewMode $w section; incr stat(texi)

#	set loading [clock clicks]
	manTextOpen $w
	texiShow $t $f $man(texinfodir)
#	$t insert js2 "$f dated [textmanip::recently [file mtime $realf]]\n\n" sc
	manTextClose $w
#	puts "loaded in [expr ([clock clicks]-$loading)/1000000.0] sec"

	if {!$keep} {pack forget $w.dups}


	# if all marks under chapter 1 (or 0), raise everyone up a level => just be robust to missing hierarchy

	### update status information

	# could always save full path here, but that occupies too much screen real estate
	set manx(man$w) $f0
	set manx(num$w) X
	set manx(hv$w) [bolg [zapZ $f] ~]
	set manx(manfull$w) $f0
	set manx(catfull$w) $f
	set manx(name$w) [file tail $f0]

	# Texinfo outline shows number of subsections (more useful than number of lines)
#	set outline [clock clicks]
	manShowManFoundSetText $w $t $manx(hv$w)
	manShowManStatus $w

#	foreach tag [lrange $manx(outline-show-v) 1 end] {$t tag raise $tag}
	# if only have one toplevel section, open it (otherwise just see single line of text)
#	if [catch {$t index js2}] {manOutline $t 0 js1} => good but superceded by showing header lines (higher level)

#	puts "outlining in [expr ([clock clicks]-$outline)/1000000.0] sec"

	manWinstdout $w $manx(manfull$w)
	manHighlights $w get
	if {[info exists manx(yview,$manx(hv$w))]} {
#puts "\amanx(yview,$f0)? => $manx(yview,$f0)"
		regexp {(.*)/(.*)} $manx(yview,$manx(hv$w)) all sect junk
		texiFault $t js$sect
#puts "\afaulting in js$sect [lindex $manx(sectname$t) [lsearch $manx(sectposns$w) $sect]]"
	}
#	focus $t	-- done by manShowManFoundSetText
	manYview $w

	return $f
}



#--------------------------------------------------
#
# manShowRfc -- use searching, etc. facilities for non-man page text
#	 to use via `send', pass full path name of text file to show
#
#--------------------------------------------------

proc manShowRfc {f0 {w .man} {keep 0}} {
	global rfcmap stat

 	manNewMode $w rfc; incr stat(rfc)

	manShowText $f0 $w $keep
#	manShowMan "$man(rfcdir)$rfcmap($num)/rfc$num.txt"
# one-time long wait (<10 sec) when show full list, then instant
# [find $man(rfcdir) "\[string match rfc.txt \$file]" {[llength $findx(matches)]==0 && $depth<=3}]
	# strip out page headers/footers
	set t $w.show
	set state [$t cget -state]
	$t configure -state normal
	while {[$t get 1.0] eq "\n"} {$t delete 1.0}
	set index 1.0
	while {[set index [$t search -forwards -regexp "^\f" $index end]]!=""} {
#puts "back @ $index"
		scan [$t index "$index linestart -1l"] "%d" back
		while {[$t get $back.0] ne "\n"} {incr back -1}
		while {[$t get $back.0] eq "\n"} {incr back -1}
		incr back
#puts "forward @ $index"
		scan [$t index "$index linestart +1l"] "%d" forward
		while {[$t compare $forward.0 < end] && [$t get $forward.0] ne "\n"} {incr forward}
		while {[$t compare $forward.0 < end] && [$t get $forward.0] eq "\n"} {incr forward}
		incr forward -1
		$t delete $back.0 "$forward.0 lineend"; # keep a \n separation (sometimes right, sometimes wrong)
	}

	# outline -- can't parse reliably
#puts "outline"
	if 0 {
	set index 1.0
	set js 1
	while {[$t get $index]!=" "} {set index [$t index $index+1l]}
	while {[set index [$t search -forwards -regexp {^[^ ]} $index+1l end]]!=""} {
#puts "outline js$js @ $index"
		$t mark set js$js $index; incr js
#		$t tag add search $index
	}
#puts "done"
	manShowManFoundSetText $w $t $f0
	}
	$t configure -state $state

	return $f0
}


#--------------------------------------------------
#
# manApropos -- show `apropos' (`man -k') information, w/dups filtered out, reformatted
#
#--------------------------------------------------

proc manApropos {name {w .man}} {
	global man manx mani stat

	if {$manx(shift)} { set manx(shift) 0; set w [manInstantiate] }
	set wi $w.info; set t $w.show

	if {$name eq ""} {set name $manx(man$w)}
	if {$name eq ""} {
		manWinstderr $w "Type in keywords to search for"
		return
	}

	set form {}
	lappend form " Apropos search for \"$name\"\n\n" {}
	manWinstdout $w "Apropos search for \"$name\" ..." 1

DEBUG {puts "manApropos: exec $man(apropos) $name $man(aproposfilter) 2>/dev/null"}
	if {[catch {set tmp [eval exec "$man(apropos) $name $man(aproposfilter) 2>/dev/null"]} info] || [string trim $tmp] eq ""} {
		if {$info ne ""} {
			lappend form $info i
		} else {
			lappend form "Nothing related found.\n" i
		}
		if {$man(glimpse)!=""} { lappend form "Try a full text search with Glimpse.\n" {}}
		set mani(apropos,cnt) 0
	} else {
		manNewMode $w aprops; incr stat(apropos)
		set mani(apropos,update) [clock seconds]
		set cnt 0
		foreach line [split $tmp "\n"] {
			# zap formatting codes if erroneously given roff source
			regsub "^\\.\[^ \t\]+" $line "" line
			# normalize tabs
			regsub -all "\[\t \]+" $line " " line
			# normalize spacing between names and descriptions to single tab
			regsub " - " $line "\t - " line
			# some idiot apropos index maker puts in "}}} {{{" before text excerpt
			regsub -all {[][{}"]} $line "" line
			# Solaris gives name of file, name of function within page
			if {[lfirst $line] eq [lsecond $line]} { regsub {^[ ]*[^ ]+[ ]*} $line "" line }

			if {[regexp "(\[^\t]+)\t+(.*)" $line all cmds desc]} {
			} elseif {[regexp "(.+)  +(.*)" $line all cmds desc]} {
			} else {set cmds [lindex $line 0]; set desc [lrange $line 1 end]}
#			foreach {cmds desc} [split $line "\t"] {} -- some apropos binaries don't write spaces?
			lappend form $cmds manref "\t$desc\n" {}
#$line "\n"
			incr cnt
		}
		.vols entryconfigure "apropos*" -state normal
# -label "apropos hit list ($cnt for \"$name\")"

		set mani(apropos,cnt) $cnt
		set manx(yview,apropos) 1/1.0
	}
	set mani(apropos,form) $form
	set mani(apropos,shortvolname) "apropos"

	manShowSection $w apropos
}



#--------------------------------------------------
#
# manHelp -- dump help text that's "compiled" offline
#
#--------------------------------------------------

proc manHelp {w} {
	global man manx stat tk_library

	set t $w.show; set wi $w.info

	manNewMode $w help; incr stat(help)
	wm title $w "$manx(title$w) v$manx(version)"
	set manx(manfull$w) "_help"
	set manx(name$w) "_help"

	manTextOpen $w
	# put in help first so marks set correctly (they stay correct subsequently as they float)
	manHelpDump $t
	# fixups
	$t tag remove h1 1.0 "1.0 lineend"; $t tag add title 1.0 "1.0 lineend"
	$t delete 2.0
#	set copyright [$t search "(c)" 1.0]
#	$t delete $copyright $copyright+3c; $t insert $copyright "\251"; $t tag add symbol $copyright
	$t image create 2.0 -image icon -padx 8 -pady 8
	$t mark set insert "author1+2lines lineend"
	$t insert insert "\t"
	$t image create insert -image face -align bottom -padx 8 -pady 8

	set demo [file join $tk_library demos widget]
	if {[file executable $demo]} {
		button $t.demo -text "run Tcl/Tk demo" -foreground $man(textfg) -background $man(textbg) -font $man(textfont) \
			-command "exec \[info nameofexecutable\] $demo &"
		$t window create [list [$t search "Tcl/Tk" 1.0]  lineend] -window $t.demo -align bottom -padx 8
	}

	# now add to opening screen
	# update warnings
	manManpathCheck
	set fWarn 0
	foreach warn {stray manpath mandesc} {
		if {$manx($warn-warnings)!=""} {
			$t insert 1.0 $manx($warn-warnings)\n\n
			set fWarn 1
		}
	}
	if {$fWarn} {$t insert 1.0 "Warnings\n\n" b}

	$t insert 1.0 $manx(updateinfo)

	manTextClose $w


	# translate SGML names to Tk marks
	set sectcnt 0; set subsectcnt 0
	foreach i [lsort -command "bytextposn $t " [$t mark names]] {
# can't ever do this because need numbers in, names out:  foreach i [lsort -dictionary [$t mark names]] {
		if {[string match "*1" $i]} { $t mark set js[incr sectcnt] $i; set subsectcnt 0; $t mark unset $i }
		if {[string match "*2" $i]} { $t mark set js$sectcnt.[incr subsectcnt] $i; $t mark unset $i }
	}

	manShowManFoundSetText $w $t "_help"
	manShowTagDist $w h2
	manHighlights $w get


	# use update and after for good interaction with rebuilding database
	update idletasks
	after 1 "manWinstdout $w \"TkMan v$manx(version) of $manx(date)\""
}

proc bytextposn {t a b} {
	return [expr {[$t compare $a < $b]?-1:[$t compare $a > $b]}]
}


#--------------------------------------------------
#
# manKeyNav -- keyboard-based navigation and searching
#    calls out to searchbox module
#
#--------------------------------------------------

proc manKeyNav {w m k} {
	global man manx

	set t $w.show; set wi $w.info
	if {[catch {set isearch [$t index isearch.first]}]} {set isearch -1}

	set firstmode [regexp "section|apropos" $manx(mode$w)]
	set casesen [expr {$firstmode?1:$man(incr,case)}]

	set fFound [searchboxKeyNav $m $k $casesen $t $wi $firstmode [expr {$manx(tryoutline$w)?"-elide":""}]]
#	if {!$fFound && $firstmode} {set fFound [searchboxKeyNav $m $k 0 $t $wi $firstmode]}
	if {[catch {set isearch2 [$t index isearch.first]}]} {set isearch2 -1}
	if {$isearch2!=-1 && $isearch!=$isearch2} {manOutlineYview $t isearch.first}

	return $fFound
}



#--------------------------------------------------
#
# manSave -- manage merging with old config file
# if passed name of save file, could put this into utils
#
#--------------------------------------------------

proc manSave {} {
	global man manx env

	if {[file dirname $manx(startup)] eq "/"} return; # no save file for root
	if {$manx(savegeom)} {set man(geom) [wm geometry .man]}
	set w [manPreferencesMake]
	if {[winfo exists $w]} {set man(geom-prefs) [geom2posn [wm geometry $w]]}

	set nfn $manx(startup)
	set ofn $manx(startup).bak

	if {![file exists $nfn] || [file writable $nfn]} {
		# thought about making backup only if current backup is more than a day old, so don't quickly propagate error to backup copy, but need backup in order to copy user code
		if {[file exists $nfn]} {file copy -force $nfn $ofn}
		if {[catch {set fid [open $nfn w]}]} {
			manWinstderr .man "$nfn is probably on a read-only filesystem"
			return
		}
		foreach p [info procs *SaveConfig] {eval $p $fid}
		puts $fid "manDot\n"
		puts $fid $manx(userconfig)

		# copy user code from old version
		if {[file exists $ofn]} {
			set ofid [open $ofn]
			set p 0
			while {[gets $ofid line]!=-1} {
				if {$p} {puts $fid $line} \
				elseif {$manx(userconfig) eq $line} {set p 1}
			}
			close $ofid
		}
		close $fid
	}

	# don't delete ~/.oldtkman
}



#--------------------------------------------------
#
# manSaveConfig -- dump persistent variables into passed file id
#
#--------------------------------------------------

proc manSaveConfig {fid} {
	global man manx high default tcl_platform tk_patchLevel

# if only persistent stuff in man array,
# could have general SaveConfig in utils.tcl

	# write out preamble
	puts $fid "#\n# TkMan v$manx(version)"
# insert $env(USER)?
	puts $fid "# configuration saved on [clock format [clock seconds]]"
	puts $fid "#"
	puts $fid "# Tcl [info patchleve]/Tk $tk_patchLevel   [winfo interps]"
	puts $fid "# $tcl_platform(os) $tcl_platform(osVersion) / $tcl_platform(machine), [winfo server .]"
	puts $fid "# screen [winfo vrootwidth .]x[winfo vrootheight .], [winfo visual .] [winfo screendepth .] / [winfo visualsavailable .]"
	puts $fid "#\n"

	set preamble {
		Elements of the man array control many aspects of operation.
		Most, but not all, user-controllable parameters are available
		in the Preferences panel.  All parameters are listed below.
		Those that are identical to their default values are commented
		out (preceded by \"#\") so that changes in the defaults will propagate nicely.
		If you want to override the default, uncomment it and change the value
		to its new, persistent setting.
	}
	foreach line [split [textmanip::linebreak $preamble] "\n"] {puts $fid "# $line"}
	puts $fid ""


	# save miscellaneous variables

	manStatsSet; # update its value
	# write out man(), commenting out if same as corresponding default()
	# maybe iterate through default array ==> no!  won't pick up man(/<directory>)'s
	foreach i [lsort [array names man]] {
		if {[info exists default($i)]} {
			set co [expr {$default($i) eq $man($i)? "#" : ""}]
			puts $fid "${co}set [list man($i)] [tr [list $man($i)] \n \n$co]"
		} elseif {[string match "/*" $i]} {puts $fid "set man($i) $man($i)"}
	}

	puts $fid "\n\n#\n# Highlights\n#\n"
#	puts $fid "# format: time, start/end pairs for use by text widget" -- too complicated to write by hand
	foreach i [lsort [array names high]] {
		puts $fid "set [list high($i)] [list $high($i)]"
	}

	puts $fid "\n"
}



#--------------------------------------------------
#
# manStats* -- cumulative statistics
#
#--------------------------------------------------

# write out new stats line
proc manStatsSaveFile {} {
	global man manx

DEBUG {puts -nonewline "manStatsSaveFile"}
	# if flag not set, don't bother
	if {$manx(statsdirty) && [file readable $manx(startup)] && [file writable $manx(startup)]} {

		# read in file
		set fid [open $manx(startup)]; set startup [read $fid]; close $fid

		manStatsSet
		foreach var {stats pagecnt} {
			set stats "set man($var) [list $man($var)]\n"

			# rewrite variable or instantiate new variable before userconfig, if such a line exists
			if {[regsub "set man\\($var\\)\[^\}\]+\}\n" $startup $stats nstartup]} {
#			if {[regsub "set man\\($var\\).*\n\}\n" $startup $stats nstartup]} -- eats too much!
				# problem copying from and into same variable, though seems to work in other cases
				set startup $nstartup
#puts "\n$startup\n"
DEBUG {puts -nonewline "\tfound existing variable"}
			} elseif {[regsub "\n\n$manx(userconfig)\n" $startup "\n\n$stats\n$manx(userconfig)\n" nstartup]} {
				set startup $nstartup
DEBUG {puts -nonewline "\tinserted before userconfig"}
			} else {
				append startup $stats
DEBUG {puts -nonewline "\tappended"}
			}
		}

		# write it out
		set fid [open $manx(startup) "w"]; puts -nonewline $fid $startup; close $fid
DEBUG {puts -nonewline "\tsaved to file"}
	}
DEBUG {puts ""}

	after [expr 5*60*1000] manStatsSaveFile
}


# save as name-value pairs so can extend list without positional dependencies leading to madness
set man(prof) {}
proc manStatsSet {} {
	global man manx stat pagecnt short prof

DEBUG {puts "manStatsSet"}
	trace vdelete stat w manStatsDirty
	trace vdelete pagecnt w manStatsDirty
	scan $man(stats) "%d" stat(cum-time)
	set newstats $stat(cum-time)

	# simple profile statistics
	set tmp {}
	foreach p [array names prof cnt-*] {
		set name [string range $p 4 end]
		lappend tmp [list $name $prof(cnt-$name) $prof(totaltime-$name)]
	}
	set man(prof) ""
	set cnt 0
	foreach p [lsort -decreasing -real -index 2 $tmp] {
		set name [lfirst $p]
		append man(prof) "\n\t" $name " \"$prof(cnt-$name) $prof(totaltime-$name)\""
		incr cnt
	}
	append man(prof) "\n"


	# bump up monolithic cumulative
	foreach i $manx(all-stats) {
		# get cumulative value
		if {[set index [lsearch $man(stats) $i]]>=0} {
			set stat(cum-$i) [lindex $man(stats) [expr {$index+1}]]
		} else {
			set stat(cum-$i) $stat($i)
		}

		# bump up cumulative totals
		incr stat(cum-$i) [expr {$stat($i)-$stat(cur-$i)}]
		set stat(cur-$i) $stat($i)

		lappend newstats $i $stat(cum-$i)
	}
	set man(stats) $newstats

	# shortcuts
	set man(shortcuts) {}
	foreach i $manx(shortcuts) {lappend man(shortcuts) [list $i $short($i)]}

	# page counts
	set man(pagecnt) ""
	set cnt 0
	# store in frequency order, so someone can easily delete entries with low counts to save space
	# create list
	set tmp {}
	foreach name [array names pagecnt] {lappend tmp [concat $name $pagecnt($name)]}; # want a destructive concat
	foreach i [lsort -index 1 -integer -decreasing $tmp] {
		set name [lfirst $i]
		# can't have sublists (sneaky stats regexp), but ok to put numbers in quotes
		append man(pagecnt) [expr {$cnt%2==0? "\n\t": " "}] $name " \"$pagecnt($name)\""
		# aw, all real editors can handle long lines
		#lappend man(pagecnt) $name [lfirst $pagecnt($name)] [lsecond $pagecnt($name)]
		incr cnt
	}
	append man(pagecnt) "\n"

	# for counts of browses, update last element
	set newchrono [lreplace $man(chronobrowse) end end "$manx(startuptime)/[expr {[clock seconds]-$manx(startuptime)}]/$manx(reading):$stat(man)/$stat(texi)/$stat(rfc)"]
	set man(chronobrowse) "\n\t"
	set cnt 0
	foreach tuple $newchrono {
		incr cnt
		append man(chronobrowse) $tuple [expr {$cnt%4==0? "\n\t": " "}]
	}
	append man(chronobrowse) "\n"


	trace variable stat w manStatsDirty
	trace variable pagecnt w manStatsDirty
	set manx(statsdirty) 0
}

# set trigger of writes to stat array to set flag
proc manStatsDirty {name1 name2 op} {
	global manx mani
DEBUG {puts "manStatsDirty: $name2"}
	set manx(statsdirty) 1
#	set mani(census,form) "" -- cleared before shown, but keep for random
}


#--------------------------------------------------
#
# Startup initialization and validity checks
#
#--------------------------------------------------


# after reading .tkman variables but before executing code

proc manDot {} {
	global manx

	manManManx

	# configuring a font should create it if necessary, as with text tags, but it doesn't
	foreach font {textpro gui guisymbol guimono peek textmono} {font create $font}
	manPrefDefaultsSet

	# make window here so ~/.tkman commands can manipulate it without `after'
	toplevel .man -class TkMan
	manPreferencesGet fill

	set manx(manDot) 1
}

proc manManManx {} {
	global man manx
	# these get set to man counterparts, unless overwritten by command line options
	foreach i {
			iconify iconname iconbitmap iconmask iconposition
			geom 
#glimpsestrays
		} {
		if {![info exists manx($i)]} {set manx($i) $man($i)}
	}
}


#
# check for existence of supporting executables, with right versions
# this information usually fine, usually just taxing startup time,
# so defer to a thread.  So have to wait a little for Statistics, who cares?
#

proc manBinCheck {{inx 0} {err 0}} {
	global man manx env stat

#	set homebin $env(HOME)/bin
#	set needmybin [expr {[file readable $homebin] && [llength [glob -nocomplain $homebin]]>0}]
# if person dosn't have ~/bin in PATH, he should know about it already (as compared to ~/man)

	# binaries checks for rman, sed, ...
	set var [lindex $manx(binvars) $inx]
	set val [string trim [set $var]]
	if {$val!=""} {
DEBUG {puts "manBinCheck on $var"}

		# first check for existence and executability...
		foreach pipe [split $val "|"] {
			set bin [lfirst $pipe]
			if {$manx(doze)} {append bin ".exe"}
			# likely check for some binaries more than once, as with gzip/gzip for compress/uncompress, but just a couple so don't mind duplicating a little work
			set found 0
#; set exe 0

#puts stderr "checking $var's $bin"
			if {[string match "/*" $bin]} {set pathlist [file dirname $bin]; set tail [file tail $bin]
			} else {set pathlist $manx(bin-paths); set tail $bin}

			foreach dir $pathlist {
				if {$dir eq "."} continue
				set fullpath $dir/$tail
				# ignore file if not executable, just like shells do
				if {[file isfile $fullpath] && [file executable $fullpath]} {
					set found 1
#					if {[file executable $fullpath]} {set exe 1}
#puts stderr "\tfound @ $dir"
					set stat(bin-$bin) $fullpath
DEBUG { puts "$bin => $fullpath" }
					break
				}
			}

			if {!$found} {
				puts stderr "$bin not found in your PATH--check the $var variable in $manx(startup) or the Makefile."
				incr err
#			} elseif {!$exe} {
#				puts stderr "$bin found but not executable--check permissions."
#				incr err
			} else {
				# ... then check for proper versions of selected executables
				if {[set info [lassoc $manx(bin-versioned) $tail]]!=""} {
#					lset $info flag minvers
					foreach {flag minvers} $info {}
					# use 2> /dev/null because glimpseindex triggers error message on FreeBSD
					set execerr [catch {set lines [exec $fullpath $flag < /dev/null 2> /dev/null]} info]
				} elseif {[string match "g*" $tail]} {
					# could be a GNU -- maybe take this out since it lengthens startup for all in exchange for small benefit for few
					set minvers 0.0
					#foreach flag {"--version" "-V" "-v"} -- takes to long to start up
					# GNU programs print version information to stderr; should be stdout
					set execerr [catch {set lines [exec $fullpath $flag < /dev/null]} info]
					set execerr 0; set lines $info
				} else continue

				if {$execerr && $minvers==0.0} {
					# nothing
				} elseif {$execerr} {
					puts "ERROR executing \"$fullpath $flag\": $info\a"
					incr err
				} else {
					set line ""; foreach line [split $lines "\n"] { if {$line!=""} break }
					# grok version number
					set vers unknown
					if {$line eq "" || ![regexp $manx(bin-versregexp) $line vers] || [package vcompare $minvers $vers]==1} {
						if {$minvers!=0.0} {
							puts stderr "$bin is version $vers--must be at least $minvers."
							incr err
						}
					} else { set stat(bin-$bin-vers) $vers }
				}
			}
		}
	}

	# chain to next one
	incr inx
	if {$inx<[llength $manx(binvars)]} {
		after 1000 manBinCheck $inx $err
	} else {
		if {$err} {exit 1}
		.occ entryconfigure "Statistics*" -state normal
	}

#	if [string match "3*" $stat(bin-rman-vers)] {set manx(rman-source) 1}
}

proc manParseCommandline {} {
	global manx argv argv0 env geometry

	for {set i 0} {$i<[llength $argv]} {incr i} {
		set arg [lindex $argv $i]; set val [lindex $argv [expr {$i+1}]]
		switch -glob -- $arg {
			-help -
			-h* -
			--help {
				set helptxt {
					[-M <MANPATH>] [-M+ <paths to append to MANPATH>] [-+M <paths to prepend to MANPATH>]
					[-[!]iconify] [-version]
					[-title <string>] [-startup <file>] [-[!]debug] [<man page>[(<section>)]]
				}
				puts -nonewline "tkman"
				foreach line [split [textmanip::linebreak $helptxt 70] "\n"] { puts "\t$line" }
				exit 0
			}
			-M {set env(MANPATH) $val; incr i}
			-M+ {append env(MANPATH) ":$val"; incr i}
			-+M {set env(MANPATH) "$val:$env(MANPATH)"; incr i}
			-dpi {set manx(dpi) $val; lappend manx(dpis) $val; incr i}
			-profile {set manx(profile) 1}
			-iconname {set manx(iconname) $val; incr i}
			-iconmask {set manx(iconmask) $val; incr i}
			-iconposition {set manx(iconposition) $val; incr i}
			-iconbitmap {set manx(iconbitmap) $val; incr i}
			-icon* {set manx(iconify) 1}
			-noicon* {set manx(iconify) 0}
			-quit {if {[string match no* $val]} {set manx(quit) 0}; incr i}
			# put the more permissive option name patterns down below
			-start* {set manx(startup) $val; incr i}
			-data* {puts stderr "-database option obsolete: database kept in memory"; incr i}
			--v* -
			-v* {puts stdout "TkMan v$manx(version) of $manx(date)"; exit 0}
			-t* {set manx(title) $val; incr i}
			-d* {set manx(debug) 1; set manx(quit) 0; set manx(iconify) 0}
			-nod* {set manx(debug) 0}
			-* {puts stdout "[file tail $argv0]: unrecognized option: $arg"; exit 1}
			default {
				after 2000 manShowMan $arg {{}} .man
				# permit several???  add extras to History?
				break
			}
		}
	}
	# grr, special case
	if {[info exists geometry]} {set manx(geom) $geometry}
}


proc ASSERT {args} {
	if {![uplevel 1 eval $args]} {
		puts "ASSERTION VIOLATED: $args"
		exit 1
	}
}

proc DEBUG {args} {
	global manx
	if {$manx(debug)} {uplevel 1 eval $args}
}

set manx(lastclick) 0
proc PROFILE {msg} {
	global manx
	set clicknow [clock clicks]
# @ [clock clicks]
	if {$manx(profile)} {puts "$msg, delta=[expr {$clicknow-$manx(lastclick)}]"}
	set manx(lastclick) $clicknow
}




##################################################
###
### start up
###
##################################################

### environment checks
if {[package vcompare [info tclversion] $manx(mintcl)]==-1 || [package vcompare $tk_version $manx(mintk)]==-1} {
	puts -nonewline stderr "Tcl $manx(mintcl)/Tk $manx(mintk) minimum versions required.  "
	puts stderr "You have Tcl [info tclversion]/Tk $tk_version"
	exit 1
} elseif {int([info tclversion])-int($manx(mintcl))>=1 || int($tk_version)-int($manx(mintk))>=1} {
	puts stderr "New major versions of Tcl and/or Tk may have introduced\nincompatibilies in TkMan.\nCheck the TkMan home site for a possible new version.\n"
}
set manx(doze) [string equal "windows" $tcl_platform(platform)]


#--------------------------------------------------
#
# set defaults for shared global variables
#
#--------------------------------------------------

# # # # # # # # # # # # # # # # # # # # # # # # # #
# DON'T EDIT THE DEFAULTS HERE--DO IT IN ~/.tkman #
# # # # # # # # # # # # # # # # # # # # # # # # # #

# man() = persistent variables
# manx() = x-pire, system use, command line overrides

set curwin [set w .man]

#
# man
#

set man(manList) {1 2 3 4 5 6 7 8 9 l o n p D}
set man(manTitleList) {
	"User Commands" "System Calls" Subroutines
	Devices "File Formats"
	Games "Miscellaneous" "System Administration" "*** check contrib/*_bindings ***"
	Local Old New Public "*** check contrib/*_bindings ***"
}
set man(stats) [clock seconds]
set man(time-lastglimpse) -1
set manx(styles) {normal bold italics bold-italics}
set manx(pts) {8 9 10 12 14 18 24 36}
set manx(sizes) {tiny small medium large larger huge}
set man(gui-family) "Times"
set man(gui-style) "bold"
set man(gui-points) "small"
set man(text-family) "New Century Schoolbook"
set man(text-style) "normal"
set man(text-points) "small"
set man(vol-family) "Times"
set man(vol-style) "normal"
set man(vol-points) "small"
#foreach f {diffa diffc diffd} {set man($f-family) $man(text-family)}
set man(diffd-style) "normal"
set man(diffc-style) "bold-italics"
set man(diffa-style) "italics"
set man(textfont) textpro

set manx(fontfamilies) [lsort [string tolower [font families]]]


### general tags
set man(isearch) {-background gray}
#set man(search) reverse
set man(search) {-background orange}; # eye-catching but not overbearing on long stretches
set man(lastposn) {-background gray90}
#set man(cmd) {-background green}
set man(autokey) {-foreground red}
set man(synopsisargs) {-background green}
set man(spot) {-background red}
set man(manref) {sanserif -foreground blue}
#set man(manrefseen) {sanserif -foreground #00C} -- not useful
# -underline yes}
set man(hot) {-foreground red}
# -underline yes}
set man(highlight) {-background #ffd8ffffb332}; # a pale yellow
set man(indent) {-lmargin1 5m -lmargin2 10m}
set man(indent2) {-lmargin1 10m -lmargin2 15m}


### Texinfo tags
# don't need to have named fonts for tags
set manx(tags) {diffa diffd diffc title h1 h2 h3 tt t sc y b bi i highlight highlight-meta search autosearchtag autokey isearch manref hot indent indent2 spot synopsisargs lastposn
r asis example smallexample display table list subscript superscript lisp smalllisp code kbd key file var emph author strong dfn chapter section subsection subsubsection majorheading titlefont heading subheading subsubheading w deffn defun defmac defspec quotation index cite subtitle author flushleft flushright center cartouche nowrap texixref rfcxref
}
#set man(diffa) {-font diffa}
#set man(diffc) {-font diffc}
#set man(diffd) {-font diffd -overstrike yes}
#set man(diffa) {"$man(diffa-family)" "$man(diffa-style)"}
#set man(diffc) {"$man(diffc-family)" "$man(diffc-style)"}
#set man(diffd) {"$man(diffd-family)" "$man(diffd-style)" -overstrike yes}
set man(diffa) {"$man(diffa-style)"}
set man(diffc) {"$man(diffc-style)"}
set man(diffd) {"$man(diffd-style)" -overstrike yes -foreground "$man(difffg)" s}
#set man(diffbar) "yes"; set manx(diffbar-v) {1 0}; set manx(diffbar-t) {"yes" "no"}
set man(strike) {-overstrike yes}
set man(tt) [set man(t) mono]
set man(sc) s
set man(y) symbol
set man(b) bold
set man(bi) bold-italics
set man(i) italics
set man(title) {bold large l}
set man(h1) {bold l}
set man(h2) {bold m}
# -spacing3 3}
set man(h3) {bold s}
set man(h4) {s}
# h1,h2,h3,h4 <=> chapter,section,subsection,subsubsection
#italics
# Texinfo -- should give these to texi.tcl
set man(r) {}
# set man(format) {} -- NO! conflicts with nroff formatting pipe!
set man(asis) {}
set man(example) {mono small s -wrap none -tabs 0.3i}
set man(lisp) {mono small -wrap none}
set man(smalllisp) {mono small s -wrap none}
set man(smallexample) $man(smalllisp)
set man(display) {-lmargin1 0.3i -lmargin2 0.3i -rmargin 0.3i}
set man(table) {-tabs 1.0i -lmargin2 1.0i}
set man(list) {-tabs 0.3i -lmargin2 0.3i}
set man(subscript) {-offset -3}
set man(superscript) {-offset 3}
set man(code) {mono small}
set man(kbd) {mono}; set man(key) $man(sc)
set man(url) {mono}
set man(file) {italics}
set man(var) {italics}
set man(emph) {italics}
set man(strong) {bold}
set man(deffn) {italics}
set man(defun) {italics}
set man(defmac) {italics}
set man(defspec) {italics}
set man(author) {bold}
set man(dfn) {bold}
set man(chapter) [set man(majorheading) [set man(titlefont) {bold m}]]
set man(section) [set man(heading) {bold s}]
set man(subsection) [set man(subheading) {italics}]
set man(subsubsection) [set man(subsubheading) {}]
set man(cite) {italics}
set man(cartouche) {-borderwidth 2}
set man(subtitle) {italics}
set man(author) {bold}
set man(center) {center}
set man(flushleft) {left}
set man(flushright) {right}
set man(w) {}
set man(nowrap) {-wrap none}
#-wrap none} -- affects whole line or nothing
set man(quotation) {-lmargin1 0.5i -lmargin2 0.5i -rmargin 0.5i}
set man(index) {s -tabs 3i}
set man(texixref) $man(manref)
set man(rfcxref) $man(manref)
set man(maxhistory) 15; set manx(maxhistory-v) [set manx(maxhistory-t) {5 10 15 20 30 40 50}]
set man(recentdays) 14; set manx(recentdays-v) [set manx(recentdays-t) {1 2 7 14 30 60 90 180}]
set man(pagecnt) {}
set man(chronobrowse) {}
# seed shortcuts with set for the beginner?  then have to remove... if they exist!
set man(shortcuts) {}
set man(shortcuts-sort) 0; set manx(shortcuts-sort-v) {1 0}; set manx(shortcuts-sort-t) {"alphabetical" "chronological"}
#set man(indexglimpse) "distributed";  -- now set in Makefile
set manx(indexglimpse-v) [set manx(indexglimpse-t) {"distributed" "unified"}]
set man(incr,case) -1; set manx(incr,case-v) {1 0 -1}; set manx(incr,case-t) {"yes" "no" "iff upper"}
set man(regexp,case) -1; set manx(regexp,case-v) {1 0 -1}; set manx(regexp,case-t) {"yes" "no" "iff upper"}
set man(maxglimpse) 200; set manx(maxglimpse-v) [set manx(maxglimpse-t) {25 50 100 200 500 1000 "none"}]
set man(maxglimpseexcerpt) 50; set manx(maxglimpseexcerpt-v) [set manx(maxglimpseexcerpt-t) {25 50 100 200 500 1000}]
# some window managers can't handle autoraise/autolower well, so default to off
set man(autoraise) 0; set manx(autoraise-v) {1 0}; set manx(autoraise-t) {"yes" "no"}
set man(autolower) 0; set manx(autolower-v) {1 0}; set manx(autolower-t) {"yes" "no"}
set man(iconify) 0; set manx(iconify-v) {1 0}; set manx(iconify-t) {"yes" "no"}
set man(focus) 1; set manx(focus-v) {1 0}; set manx(focus-t) {"window entry" "click-to-type"}
set man(subsect) ""; set manx(subsect-v) {"-b" ""}; set manx(subsect-t) {"yes" "no"}
set man(nroffsave) "off"; set manx(nroffsave-v) [set manx(nroffsave-t) {"on" "on & compress" "off" "off & ignore cache"}]
# roff so pokey never too fast to cache, though could put it in can wait for hardware to catch up
#set man(toofasttocache) "1.5"; set manx(toofasttocache-v) [set manx(toofasttocache-t) {"on" "on & compress" "off" "off & ignore cache"}]
set man(headfoot) "-k"; set manx(headfoot-v) {"-k" ""}; set manx(headfoot-t) {"yes" "no"}
set man(hyperclick) "single"; set manx(hyperclick-v) [set manx(hyperclick-t) {"double" "single"}]
set man(fsstnddir) /var/catman
set man(fsstnd-always) 0; set manx(fsstnd-always-v) {1 0}; set manx(fsstnd-always-t) {"always" "iff .../canN unwritable"}
set man(versions) {}
set man(aproposfilter) {| sort | uniq}
set man(scrollbarside) right; set manx(scrollbarside-v) [set manx(scrollbarside-t) {"left" "right"}]
set man(documentmap) 1; set manx(documentmap-v) {1 0}; set manx(documentmap-t) {"yes" "no"}
set man(strictmotif) 0; set manx(strictmotif-v) {1 0}; set manx(strictmotif-t) {"yes" "no"}
set man(subvols) 1; set manx(subvols-v) {1 0}; set manx(subvols-t) {"yes" "no"}
#set man(checkstraycats) 1; set manx(checkstraycats-v) {1 0}; set manx(checkstraycats-t) {"yes" "no"} -- doesn't matter so much as startup really fast, and better to be safe than sorry
set man(preferGNU) ""; set manx(preferGNU-v) {"g?" ""}; set manx(preferGNU-t) {"yes" "no"}
set man(preferTexinfo) 1; set manx(preferTexinfo-v) {1 0}; set manx(preferTexinfo-t) {"yes" "no"}
set man(wordfreq) 0; set manx(wordfreq-v) {1 0}; set manx(wordfreq-t) {"yes" "no"}
set man(search,fcontext) 0; set manx(search,fcontext-v) [set manx(search,fcontext-t) {0 1 2 3 4 5}]
set man(search,bcontext) 0; set manx(search,bcontext-v) [set manx(search,bcontext-t) {0 1 2 3 4 5}]
set man(textboxmargin) 5; set manx(textboxmargin-v) [set manx(textboxmargin-t) {0 1 2 3 4 5 7 10}]
set man(lengthchunk) "line"; set manx(lengthchunk-v) {line screen page ""}; set manx(lengthchunk-t) {"lines" "screenfuls" "printed pages" "no report"}; # "line/screen"
set manx(line-scale) 1; set manx(screen-scale) 45; set manx(page-scale) [expr int(60*1.5)]
set man(error-effect) "bell & flash"; set manx(error-effect-v) [set manx(error-effect-t) {"bell & flash" "bell" "flash" "none"}]
set man(columns) 65; set manx(columns-v) {65 90 130 5000}; set manx(columns-t) {"65 (most compatible)" 90 130 "wrap to screen width"}; # no one would want shorter lines
set manx(longtmp) /tmp/ll
set man(volcol) 4.0c; set manx(volcol-v) {0 1.5c 2.0c 2.5c 3.0c 3.5c 4.0c 4.5c 5.0c 7.5c 10.0c}; set manx(volcol-t) {"no columns" "1.5 cm" "2 cm" "2.5 cm/~1 inch" "3 cm" "3.5 cm" "4 cm" "4.5 cm" "5.0 cm/~2 inches" "7.5 cm" "10 cm"}
set man(apropostab) "4.5c"; set manx(apropostab-v) {0 3.0c 4.0c 4.5c 5.0c 5.5c 6.0c 7.5c 10.0c}; set manx(apropostab-t) {"none" "3 cm" "4 cm" "4.5 cm" "5 cm" "5.5 cm" "6 cm" "7.5 cm" "10 cm"}
#set man(showoutsub) ""
set man(high,hcontext) 50
set man(high,vcontext) 5; set manx(high,vcontext-v) [set manx(high,vcontext-t) {0 2 5 7 10 15 20}]
set man(outline) "allbut"; set manx(outline-v) {"allexp" "allcol" "allbut" "off"}; set manx(outline-t) {"all expanded" "all collapsed" "all collapsed but" "none"}
# long Name, short Description, short Synopsis, short See Also
set man(outlinebut) {^name\d+$|(syntax|synopsis)[1-2]?\d$|description\d$|(author|identification|credits)\D*[1-9]$|(see also|related information)\d$}
set man(autosearchnb) {unsafe|warning|danger|obsolete|international|english|posix|web|performance|privacy|security|authoritative|deadlock|berkeley|solaris|version \d+}
# nonnb - if you're in the area, show me, but not of global importance
# me -posix, +annotation, +texinfo=>superceded by authoratative
# for others
#	+multithread
# too frequent to show as Notemarks -- but good to see if you happen to be in the area
#	@ - either no email or million in list of contributors + many false hits + meta character for Texinfo and Perl
set man(autosearch) {fail|ignore|explicit|difficult|tricky|postscript|html|priority|wait|block}
# maybe +octal
set man(autokeywords) {default|return|null|empty|inclusive|exclusive|sensitive|insensitive|ascii|octal|hex|dec |decimal|symbolic}; # not dec=>december
# highlights and searches change, but subsections and options are stable
foreach i {highlight options search manfill manref} {
# subsect
	set manx($i-show-v) {"alwaysvis" "firstvis" "never"}
	set manx($i-show-t) {"always" "at first" "never"}
}
set manx(openFilePWD) [pwd]
set manx(pathsep) [expr {$manx(doze)?";": ":"}]; # Tcl seems not to have addressed this
set man(randomscope) all
set manx(manhits) {}
set man(tidyout) 1; set manx(tidyout-v) {1 0}; set manx(tidyout-t) {"yes" "no"}
set manx(highlight-show-v) {"halwaysvis" "firstvis" "never"}
set manx(search-show-v) {"salwaysvis" "sfirstvis" "never"}; # is "never" never a good choice?
set manx(manfill-show-v) {"malwaysvis" "mfirstvis" "never"}
set manx(show-atags) {alwaysvis halwaysvis salwaysvis malwaysvis}
set manx(show-ftags) {firstvis mfirstvis sfirstvis lastposn}
set manx(show-tags) [concat lastposn $manx(show-atags) $manx(show-ftags)]
set man(highlight-show) halwaysvis; #firstvis
#set man(subsect-show) never
set man(options-show) firstvis
set man(manref-show) never
set man(search-show) salwaysvis
set man(manfill-show) mfirstvis
set man(manfill-sects) {description introduction command usage operators}
#set man(outline-show) section; set manx(outline-show-v) [set manx(outline-show-t) {"chapter" "section" "subsection" "subsubsection"}]
# DON'T ADD THESE TO man(manfill-sects)
#	environment - you consult to set once, then ignore
set man(manfill) "in entirety"; set manx(manfill-v) [set manx(manfill-t) {"in entirety" "as space"}]
set man(showsectmenu) 1; set manx(showsectmenu-v) {1 0}; set manx(showsectmenu-t) {"yes" "no"}
set man(rebus) 0; set manx(rebus-v) {1 0}; set manx(rebus-t) {"yes" "no"}
set man(tryfuzzy) 1; set manx(tryfuzzy-v) {1 0}; set manx(tryfuzzy-t) {"yes" "no"}
set man(showrandom) 0; set manx(showrandom-v) {1 0}; set manx(showrandom-t) {"yes" "no"}
set man(maxpage) 0; set manx(maxpage-v) {1 0}; set manx(maxpage-t) {"yes" "no"}
##always show highlights as combo +/-/menu
set man(geom) [min 570 [expr {[winfo screenwidth .]-100}]]x[min 800 [expr {[winfo screenheight .]-50}]]+100+10
set man(geom-prefs) +300+300
set man(iconname) {TkMan: $manx(name$curwin)}
set man(iconbitmap) "(default)"
set man(iconmask) ""
set man(iconposition) ""
set man(startup) $env(HOME)/.tkman

# colors
set man(colors) {black white red "light yellow" yellow orange green blue beige SlateGray4 gray75 gray90}

# set preferred colors
set man(textfg) "black"
set man(textbg) "white"
set man(difffg) "gray60"
#set man(buttfg) [set man(activefg) [set man(guifg) "gray"]]
#set man(buttbg) [set man(guibg) "beige"]
#set man(activebg) #eed5b7

# colors are Tk's defaults, except for black-on-white text
checkbutton .a
#set man(textfg) []
set man(buttfg) [set man(guifg)  [.a cget -foreground]]
set man(guibg) [set man(buttbg) [.a cget -background]]
#set man(textbg) "white"; if {$man(textfg) eq "white"} {set man(textbg) "black"}
# default textbg is ugly grey
set man(activefg) [.a cget -activeforeground]
set man(activebg) [.a cget -activebackground]
destroy .a
set man(selectionfg) black
set man(selectionbg) gray90

#set man(fontpixels) "X"
#set manx(fontpixels-v) {"-" "+"}; set manx(fontpixels-t) {"pixels" "points"}
set manx(dpi) [expr {int([winfo screenwidth .]/([winfo screenmmwidth .]/25.4))}]
## for intial setting, assume 75 and 100 dpi fonts available
##set man(fontpixels) [expr {($manx(dpi)==75 || $manx(dpi)==100)? "": "-"}]
# set interactively if no startup file
# find closest font dpi
if {[catch {set bestdpi $env(DISPLAY_DPI)}]} {
	set bestdpi 75; set mindpidiff 1000
	foreach dpi $manx(dpis) {
		set dpidiff [expr {abs($dpi-$manx(dpi))}]
		if {$dpidiff<$mindpidiff} {set mindipdiff $dpidiff; set bestdpi $dpi}
	}
}
tk scaling [expr {$bestdpi/72.0}]
# can't rescale
# tk scaling can be overridden in ~/.tkman free essay portion
# if you want to use pixels, use "tk scaling 1"


set manx(mono) [expr {[winfo depth .]==1}]
if {$manx(mono)} {
	set man(foreground) "black"
	set man(background) "white"
	set man(colors) {black white}
	#set man(textfg) []
	set man(activebg) [set man(buttfg) [set man(guifg) "black"]]
	#set man(textbg) []
	set man(difffg) [set man(activefg) [set man(buttbg) [set man(guibg) "white"]]]
	set man(selectionfg) $man(textbg); set man(selectionbg) $man(textfg); # gotta reverse on b&w

	set man(search) [set man(isearch) "reverse"]
#	set man(cmd) underline
	set man(highlight) "bold-italics"
	set man(manref) "mono underline"
#	set man(manrefseen) "mono"
	set man(spot) reverse

	# any more modifications for monochrome?
}
set man(highlight-meta) $man(highlight)


### collect all man's to this point as the defaults
# used for Defaults button in Preferences
# and to "comment out" unchanged parameters, so that subsequent
# changes (corrections), usually in the Makefile, propagate as expected
# (i.e., should make for fewer cases of "delete ~/.tkman")
# CAREFUL!  Only man()'s recorded here in defaults are saved to ~/.tkman
# ok to put man() with modules, 'cause all modules loaded by this time
set man(print) [string trim $man(print)]
foreach i [array names man] {set default($i) $man($i)}


#
# man()-independent manx()
#

set manx(title) "TkMan"
regexp {(\d\d\d\d)/(\d\d)/(\d\d)} {$Date: 2003/04/01 23:02:52 $} manx(date) y m d
set manx(mtime) [clock scan "$m/$d/$y"]
set manx(stray-warnings) ""
if {[catch {set default(manList) 0}]} {puts "\aBLT conflicts with TkMan."; exit 1}
set manx(manList) $man(manList)
set manx(manTitleList) $man(manTitleList)
set manx(userconfig) "### your additions go below"
set manx(posnregexp) {([-+]?\d+)([-+]\d+)}
set manx(bkupregexp) {(~|\.bak|\.old)}; # exclude old?  may want to refer to it => make it an option?
set manx(init) 0
set manx(texishowsubs) ""
set manx(manDot) 0
set manx(cursor) left_ptr
set manx(yview,help) 1/1.0
set manx(paths) ""
set manx(pathstat) ""
set manx(uid) 1
set manx(hunkid) ""
set manx(outcnt) 1
set manx(debug) 0
set manx(profile) 0
set manx(defaults) 0
set manx(startup) $man(startup)
set manx(savegeom) 1
# can't be sure volume 1 exists(!)
#set manx(lastvol$w) 1
set manx(lastman) TkMan
set manx(censussortby) 1
set manx(highsortby) 0
set manx(normalize) "-N"
set manx(quit) 1
set manx(mandot) ""
set manx(db-manpath) ""
set manx(db-signature) ""
set manx(mondostats) 0
set manx(newmen) ""
set manx(highdontask) {}
#set manx(xmono) {courier|helvetica}
set manx(subdirs) "{sman,man,cat}?*"
set manx(shift) 0
set manx(searchtime) 0
if {[file executable /usr/ucb/whoami]} {set manx(USER) [exec /usr/ucb/whoami]
} elseif {[file executable /usr/bin/whoami]} {set manx(USER) [exec /usr/bin/whoami]
} elseif {[info exists manx(USER)]} {set manx(USER) $env(USER)
} else {set manx(USER) "luser"}
append man(autosearchnb) "|$manx(USER)"; # vanity
# default printer
if {[info exists env(PRINTER)]} {set manx(printer) $env(PRINTER)} \
elseif {[info exists env(LPDEST)]} {set manx(printer) $env(LPDEST)} \
else {set manx(printer) ""}

# versions of supporting binaries, if those binaries happen to be used
set manx(bin-versregexp) {(\d+\.\d+(\.\d+)*)}; # followed by ([^,;:\s]*)
# Glimpse min version of 4.0 so know what index looks like for index pseudovolume
# morons maintaining glimpse exit with non-zero for version information
#set manx(bin-versioned) {{rman "-v" 3.0.5} {glimpse "-V" 4.0} {glimpseindex "-V" 4.0}}
set manx(bin-versioned) {{rman "-v" 3.1}}
set manx(modes) {man texi rfc help section txt apropos glimpse info}
set manx(mode-desc) {"manual pages seen" "Texinfo files read" "RFC documents read" "references to help page" "volume listings invoked" "text files seen" "apropos listings" "Glimpse full-text searches" "ganders at this page"}
DEBUG { assert [llength $manx(modes)]!=[llength $manx(mode-desc)] "mismatched modes lists" 1 }
set manx(stats) {man-hyper man-dups man-button man-history man-shortcut man-random page-section page-highlight-go page-highlight-add page-highlight-sub page-shortcut-add page-shortcut-sub page-regexp-next page-regexp-prev page-incr-next page-incr-prev page-downsmart page-mono high-exact high-move high-lose high-carry print glimpse-builds outline-expand outline-collapse instantiation checkpoint}
set manx(stats-desc) {"via hyperlink" "via multiple matches pulldown" "via man button or typein box" "via history menu" "via shortcut menu" "via random page" "jumps to section header" "jump to highlight" "highlight additions" "highlight deletions" "shortcut additions" "shortcut deletions" "regular expression searches forward" "regular expression searches backward" "incremental searches forward" "incremental searches backward" "scrolls with smart outline expansion/collapse" "swaps between proportional font and monospaced" "exact" "repositioned" "lost" "pages printed" "Glimpse builds" "expanded outline" "collapsed outline" "additional instantiations" "checkpoints to save file"}
DEBUG { assert [llength $manx(stats)]!=[llength $manx(stats-desc)] "mismatched stats lists" 1 }
set stat(man-no) 0
set manx(all-stats) [concat $manx(modes) $manx(stats) "man-no" "executions"]
foreach i $manx(all-stats) {set stat(cur-$i) [set stat($i) 0]}
set manx(zdirsect) {(?:man|sman|cat)\.?(.*)}



# TKMAN environment variable gives standard options
# env(TKMAN) goes first so options given on command-line override
if {[info exists env(TKMAN)]} {set argv "$env(TKMAN) $argv"}
if {![info exists env(PATH)] || $env(PATH) eq ""} { set env(PATH) "/usr/local/bin:/usr/bin" }
# don't pick up changes to env(PATH) in startup file (just so you know)
set manx(bin-paths) [split [string trim $env(PATH) $manx(pathsep)] $manx(pathsep)]

#puts "*** manParseCommandline"
# usually want to do this after startup file, but need to get -M here, before manx(startup) (?)
manParseCommandline
set manx(startup-short) [file tail $manx(startup)]

# execute some arbitrary Tcl code for new users (it may create a startup file)
if {![file exists $manx(startup)]} {eval $manx(newuser)}

# read in startup file after proc/vars/ui, so can modify them
set manx(savefilevers) "1.0"
set manx(updateinfo) ""
#puts "*** before startup"

# if on special case OS and don't have startup file, instantiate right startup code
# uname -s -r: SunOS 5.5 on ecstasy, IRIX 5.2 on bohr, SCO_SV on SCO OpenServer Release 5
# (for future references: OSF1 V3.2 on tumtum, HP-UX A.09.05 on euca)
# Put this code after eval newuser.

if {$manx(startup) ne "" && ![file exists $manx(startup)] && [file writable [file dirname $manx(startup)]]} {
	catch {
	set os [string tolower $tcl_platform(os)]; set osvers $tcl_platform(osVersion)
	set setup ""; # most OSes work without configuration file

	# three trouble makers
	if {$os eq "sunos" && $osvers>=5.0} {
		set setup solaris
		if {$osvers>=5.7} {append setup "28"
		} elseif {$osvers>=5.6} {append setup "26"
		} elseif {$osvers>=5.5} {append setup "25"}
	} elseif {[string match "irix*" $os]} {
		set setup irix
	} elseif {$os eq "sco_sv"} {
		set setup $os
	}
#puts stderr "\afound $setup"

	if {$setup!="" && [info exists manx($setup-bindings)]} {
		set fid [open $manx(startup) "w"]
		puts $fid $manx($setup-bindings)
		close $fid
	}
   }
}



if {$manx(startup)!="" && [file readable $manx(startup)]} {
	set fid [open $manx(startup)]

	# I don't know how this happens, but it has, apparently
	if {[string match "#!*" [gets $fid line]]} {
		puts stderr "$manx(startup) looks like an executable."
		puts stderr "You should delete it, probably."
		exit 1
	}

	while {![eof $fid]} {
		# manx(savefilevers) discards alpha/beta designation
		if {[regexp {^# TkMan v(\d+(\.\d+)+)} $line all savefilevers]} {
			set manx(savefilevers) $savefilevers
			break
		}
		gets $fid line
	}
	catch {close $fid}
DEBUG {puts "*** savefilevers = $manx(savefilevers)"}

	source $manx(startup)

#puts "*** read $manx(startup)"
# later use array {get,set} => no, hard for user to edit

	# update several variables from old save files

	set fixup 0
	if {[package vcompare $manx(savefilevers) 1.6]==-1} {
		set manx(updateinfo) "Startup file information updated from version $manx(savefilevers) to version $manx(version).\n"

		### changes for < 1.6
		# update these variables
		foreach var {catsig compress zcat} {
#	 detailed list of changes more information than user wants
#			append manx(updateinfo) "    Changed man($var) from $man($var) to $default($var)\n"
			set man($var) $default($var)
		}

		# zap problem shifted sb keys
		foreach k {greater less question} {
			set var "sb(key,MS-$k)"
			if {[info exists $var]} {unset $var}
#			append manx(updateinfo) "    Deleted $k keybinding (use M-$k)\n"
		}

		# call manDot before user code
		append manx(updatedinfo) "    Added call to manDot\n"
		# (if no problem before, no problem now)

		# zap old man() variables ==> done every time save file updated
#		foreach v [array names man] {
#			if {![info exists default($v)] && ![string match "/*" $v]} {
#				append manx(updateinfo) "    Deleted obsolete variable man($v) (was set to $man($v))\n"; 
#				unset man($v)
#			}
#		}

		append manx(updateinfo) "Save updates via the Quit button or Occasionals/Checkpoint, or cancel updates via \"Occasionals / Quit, don't update\".\n"
		# backup old startup file
		if {![catch "file copy -force $manx(startup) [set ofn $manx(startup)-$manx(savefilevers)]"]} {
			append manx(updateinfo) "Old startup file saved as $ofn\n"
		}
		append manx(updateinfo) "\n\n"
		set fixup 1

	}

	if {[package vcompare $manx(savefilevers) 1.8.1]==-1} {
		if {[info exists man(stats)] && [llength [lsecond $man(stats)]]==2} {
			# construct and save new version soon
			set newstyle [lfirst $man(stats)]
			foreach s [lrange $man(stats) 1 end] { lappend newstyle [lfirst $s] [lsecond $s] }
			set man(stats) $newstyle

			# use of glimpse has changed: -W, no -w for glimpse; -f for glimpseindex
			set man(glimpse) $default(glimpse)
			set man(glimpseindex) $default(glimpseindex)
			# likewise, reset manformat so assured of picking up long lines
			set man(format) $default(format)

			set fixup 1
		}
	}

	if {[package vcompare $manx(savefilevers) 2.0]==-1} {
		foreach db [glob -nocomplain ~/.tkmandatabase*] {file delete -force -- $db}
	}

	if {[package vcompare $manx(savefilevers) 2.1]==-1} {
		set man(high,vcontext) 5
		# lose existing setting, but how else to split into nb and non-nb?
		set man(autosearch) $default(autosearch); # autosearchnb and autokeywords are new in 2.1
	}

	# convert shortcuts to name+time added, the time for use later
	if {[llength [lfirst $man(shortcuts)]]==1} {
		set tmp {}
		foreach i $man(shortcuts) {lappend tmp [list $i [clock seconds]]}
		set man(shortcuts) $tmp
	}
	if {[info exists man(pagecnt)] && [llength [lsecond $man(pagecnt)]]==1} {
		set tmp {}
		foreach {name cnt lastread} $man(pagecnt) {lappend tmp $name [list $cnt $lastread]}
		set man(pagecnt) $tmp
	}
	array set pagecnt $man(pagecnt)

	if {!$manx(manDot)} {
		#puts stderr "no mandot" -- clean this up silently
		manDot
		set fixup 1
	}

	if {$fixup} {after 500 manSave}
} else {
# if no startup file
}

#if {$man(fontpixels)=="X"} {
#	set man(fontpixels) "+"
#	after 100 {.__tk__messagebox.msg configure -font {Times 24}}
#	if {[tkMessageBox -message "Is this font jagged?" -type yesno -icon question]=="yes"} {
#		set man(fontpixels) "-"
#	}
#}

if {$man(glimpse) eq "" || $man(glimpseindex) eq ""} {set man(glimpse) ""; set man(glimpseindex) ""}

#if {[lsearch $man(versions) $manx(version)]==-1} {lappend man(versions) $manx(version)} -- track backtracks too
if {[lindex $man(versions) end]!=$manx(version)} {lappend man(versions) $manx(version)}

set manx(shortcuts) {}
foreach i $man(shortcuts) {
	foreach {name time} $i {lappend manx(shortcuts) $name; set short($name) $time}
}

catch {unset high(*)}


foreach {name info} $man(prof) {
	set prof(cnt-$name) [lfirst $info]; set prof(totaltime-$name) [lsecond $info]
}


# manx(highs) needs to get latest list of colors
set manx(highs) [concat $manx(styles) reverse underline mono $man(colors)]

set manx(extravols) {}
if {$man(apropos) ne ""} {
	lappend manx(extravols) [list apropos "apropos hit list" [list "No apropos list" b]]
}
if {$man(glimpse) ne ""} {
	lappend manx(extravols) [list glimpse "glimpse hit list" [list "No glimpse list" b]]
	if {$man(indexglimpse)=="unified" && [file readable [file join $man(glimpsestrays) ".glimpse_index"]]} {
		lappend manx(extravols) [list "glimpseindex" "glimpse word index" {}]
	}
}
lappend manx(extravols) {recent "Recently added/changed" {}} {high "All with Highlights" {}} {census "All pages seen" {}} {all "All Enabled Volumes" {}}
foreach i $manx(extravols) { lappend manx(specialvols) [lfirst $i] }

set manx(subregexp) {js(\.?(\d+))+}
set manx(supregexp) {js(\d+(\.\d+)*)\.\d+}
# calculate compression comparison expressions
set manx(zregexp) "\\.("; set manx(zregexpopt) "(\\.("
set manx(zglob) "{"; set manx(zoptglob) "{,"
foreach z $man(zlist) {
	append manx(zregexp) "$z|"
	append manx(zregexpopt) "$z|"
	append manx(zglob) "$z,"
	append manx(zoptglob) ".$z,"
}
foreach z {zregexp zglob zoptglob} { set manx($z) [string trimright $manx($z) ",|"] }
set manx(zregexp0) "[string trimright $manx(zregexp) |])"
set manx(zregexpl) "(\\.\[^\\. \]+)$manx(zregexp0) "
set manx(zregexp) "$manx(zregexp0)\$"
set manx(zregexpopt) "[string trimright $manx(zregexpopt) |]))?"
append manx(zglob) "}"; append manx(zoptglob) "}"
set manx(filetypes) [list [list "Manual Pages" ".{\\\[1-9olnpD],man}*$manx(zoptglob)"] [list "Texinfo" ".texi$manx(zoptglob) .texinfo$manx(zoptglob)"] [list "Text file" ".txt$manx(zoptglob)"] {"Any file" *}]


# make master lists of man, bin dirs
set mani(MASTER,dirs) {}
foreach i $manx(mastermen) {
	if {![file readable $i] || [lsearch $manx(paths) $i]!=-1} continue
#puts "master $i"; flush stdout
	foreach j [glob -nocomplain $i/man*] {
		if {[file readable $j] && [file isdirectory $j]} {lappend mani(MASTER,dirs) $j}
	}
}
set manx(aux-binpaths) {}
foreach i $manx(masterbin) {
	if {[lsearch $manx(bin-paths) $i]==-1 && [file readable $i] && [file isdirectory $i]} {
		lappend manx(aux-binpaths)
	}
}




# do this after source of ~/.tkman has chance to change some of these
set manx(binvars) {
	manx(rman) man(glimpse) man(glimpseindex) man(cksum) man(gzgrep)
# man(co) man(rlog) man(rcsdiff) man(vdiff) -- don't check for these
	man(format) man(print) man(catprint) man(apropos) man(zcat) man(compress) 
}

set manx(canformat) [expr {$man(format) ne ""}]
if {$manx(doze)} {
    # Windoze doesn't have groff/nroff/tbl/neqn/col), glimpse, lpr, ...
    # so can't format pages, no full-text search, can't print, ...
    foreach var {format glimpse glimpseindex print catprint apropos} {
	set man($var) ""
    }

    # convert PATH and MANPATH
    # cygwin bash uses "//C/bin://C/jdk1.2.2/bin",
    # whereas Tcl wants "C:/bin;C:/jdk1.2.2/bin"
    foreach var {MANPATH PATH} {
	if {[info exists env($var)] && [string match "*//*" $env($var)]} {
	    set newvar $env($var)
	    regsub -all ":" $newvar "\;" newvar
	    regsub -all {//([A-Z])/} $newvar {\1:/} newvar
	    set env($var) $newvar
	}
    }
}

after 1500 manBinCheck

# assertions

if {[llength $man(manList)]!=[llength $man(manTitleList)]} {
	puts stderr "Length of section abbreviations differs from length of section titles:\n\nlength [llength $man(manList)]:\t$man(manList)\n\nlength [llength $man(manTitleList)]:\t$man(manTitleList)"
	exit 1
}

# no sense to tease glimpseindex if can't write anything
set manx(glimpseindex) ""
if {$man(glimpse)!="" && $man(glimpseindex)!=""} {
	foreach p $manx(paths) {
		if {![file writable $p]} {set manx(glimpseindex) $man(glimpseindex); break}
	}
}


# almost always you'll want to make the cross product
#	 if you don't, be tricky and `set manx(defaults) 1'
if {!$manx(defaults)} manDescDefaults

# set up cumulative statistics watchdog
lappend man(chronobrowse) "X"
set manx(statsdirty) 0
manStatsSet
# added this statistic after the others
#if {[package vcompare $manx(savefilevers) 2.0.3]==-1} {set stat(cum-executions) [expr $stat(cum-executions)/$stat(cum-help)]; set man(stats) "executions $stat(cum-executions) $man(stats)"}
if {[package vcompare $manx(savefilevers) 2.0.3]==-1} {set stat(cum-executions) $stat(cum-help); set man(stats) "executions $stat(cum-executions) $man(stats)"}
#if {$stat(cum-executions)==0 && $stat(cum-help)>0} {set stat(cum-executions) $stat(cum-help)}
manStatsSaveFile;	# should just set timer this time through, not save to a file

# if no stray dirs for glimpse, don't glimpse there
#if {$man(indexglimpse)=="distributed" && [llength $mani($manx(glimpsestrays),dirs)]==0} {set manx(glimpsestrays) ""}


if 0 {
foreach proc [info procs man*] {
	puts -nonewline "rewriting $proc"
	set vals ""; foreach aargh [info args $proc] {append vals " \$$aargh"}
	set aarghs {}
	foreach aargh [info args $proc] { if {[info default $proc $aargh def]} {lappend aarghs [list $aargh $def]} else {lappend aarghs $aargh}}
puts "   $aarghs"
	proc $proc $aarghs "puts \"entering $proc $vals\"; flush stdout\n[info body $proc]\nputs {exiting $proc}; flush stdout"
}
}

set STOP 0


TkMan
#image create bitmap wmicon -data [icon cget -data]

set starttime [time manInit]
manHelp $w; # show newly available stray cat warnings


# debug box always available
# convenience variables
#	set w .man
set t $w.show

# debug box
entry $w.in -relief sunken -textvariable manx(debugcmd)
# maybe automatically decide destination based on length
bind $w.in <KeyPress-Return> {manWinstdout .man "[eval $manx(debugcmd)]"}
bind $w.in <Meta-KeyPress-Return> {manTextOpen .man; $t insert end $manx(debugcmd) b "   =>   " tt "\n\n" {} [eval $manx(debugcmd)]; manTextClose .man}

DEBUG {
	puts stdout "init takes $starttime"
	pack $w.in -fill x
	proc manStatsSaveFile args {}
}


set stat(executions) 1

# no profiling on non-TkMan procs
#rename proc {}
#rename proc_builtin proc

if {$stat(cum-executions)==100 
	|| ($stat(cum-executions)>0 && int($stat(cum-executions)/1000)*1000==$stat(cum-executions))} {
	set txt "This is the $stat(cum-executions)th time you've run TkMan.  Please help improve future versions of TkMan by emailing the file ~/.tkman with its collected statistics to phelps@ACM.org.  (This must be done manually; no action is taken automatically.)  Thanks!"
	manTextPlug $w.show 1.0 "$txt\n\n" b
	tk_messageBox -icon info -parent $w -title "Please Help" -type ok -message $txt
}

# report first run and every month or so
if {$stat(cum-executions)%27==0} {
	set txt ""
	if {$man(texinfodir)==""} {append txt "+ In addition to showing manual pages, TkMan supports Texinfo (GNU documentation) with high quality text display, intra-document and full text search, highlight annotations, and an outlining interface that preserves navigation context to combat (solve?) the \"lost in info-space\" problem.  (To install, see the Makefile.)\n"}
	if {$man(glimpse) eq ""} {append txt "+ You can do full text search on all man pages, Texinfo, and other text files by installing Glimpse.  (It's is conviently available as binaries.  To install, see the Makefile.)\n"}
	if {$man(outline) eq "off"} {append txt "+ Now that you're comfortable with TkMan, try the outlining display (set Preferences/Outline/Outlining state at page load, to all collapsed but).  Rather than scolling screen after screen in a man page to find the information, outlining and Notemarks give a high density display that's likely to show the information you want on the first or second screenful.\n"}
	if {$stat(cum-executions)>10 && $stat(cum-page-highlight-add)<10} {append txt "+ You can make yellow highlighter annotations.  If the page content changes or the page moves, highlights are automatically repositioned.\n"}
	if {$stat(cum-executions)>10 && $man(outline)!="off" && $stat(cum-page-downsmart)<$stat(cum-man)/2} {append txt "+ Try using <Return> for smart scrolling through outlines, opening up closed sections as they are encountered and closing up sections as they are read.\n"}
	if {!$man(wordfreq)} {append txt "+ You can get a kind of summary of man pages via word frequency counts (turn on at Preferences/See/Page summary).\n"}
	if {$man(columns)!=5000} {
		append txt "+ You can man page lines to wrap at whatever your screen width is (set Preferences/Database/Width of man pages)."
		if {![string match "*groff*" $man(format)]} {append txt "  (But first you need to use groff, set in the Makefile or your ~/.tkman file)."}
  		append txt "\n"
	}
	if {$txt!=""} {
		tk_messageBox -icon info -parent $w -title "Presents for you" -type ok -message "TkMan:  There are a couple great features you're not taking advantage of.  Click OK to read more."
		manTextPlug $w.show 1.0 "$txt\n\n" b
		wm deiconify $w
	}
}