Source

oo-browser / br.el

   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
;;!emacs
;;
;; FILE:         br.el
;; SUMMARY:      Browse object-oriented code.
;; USAGE:        GNU Emacs Lisp Library
;; KEYWORDS:     matching, oop, tools
;;
;; AUTHOR:       Bob Weiner
;; ORG:          BeOpen.com
;;
;; ORIG-DATE:    12-Dec-89
;; LAST-MOD:     10-May-01 at 20:49:01 by Bob Weiner
;;
;; Copyright (C) 1989-1998  BeOpen.com
;; See the file BR-COPY for license information.
;;
;; This file is part of the OO-Browser.
;;
;; DESCRIPTION:  
;; DESCRIP-END.

;;; ************************************************************************
;;; Other required Elisp libraries
;;; ************************************************************************

(require 'br-lib)

;;; ************************************************************************
;;; Customization
;;; ************************************************************************

(defgroup oo-browser nil
  "Multi-Language Object-Oriented Code Browser"
  :group 'tools
  :prefix "br-")

(defconst br-feature-signature-regexp "[:|,]"
  "Regular expression that matches a feature signature but not a class name.")

(defcustom br-inherited-features-flag t
  "*If non-nil (the default), feature/element listings include all inherited features.
If nil, only those features lexically included within a class are shown."
  :group 'oo-browser
  :type 'boolean)

(defcustom br-protocols-with-classes-flag t
  "*If non-nil (the default), protocols (interfaces) are included in listings of all classes or top-level classes."
  :group 'oo-browser
  :type 'boolean)

(defcustom br-inhibit-version nil
  "*If non-nil, skips version/credit information upon startup.
The default should be left as nil, since new users may find this helpful."
  :group 'oo-browser
  :type 'boolean)

(defcustom br-invert-ancestors nil
  "*If non-nil makes ancestors appear as do other inheritance listings.
That is, parents appear above children, rather than the default, which is the
reverse."
  :group 'oo-browser
  :type 'boolean)

(defcustom br-keep-viewed-classes nil
  "*If non-nil means leave all viewed classes around for later selection.  
A nil value causes deletion of the last viewed class buffer whenever a new
one is displayed.   Note this does not affect classes displayed for editing,
all such classes are left around."
  :group 'oo-browser
  :type 'boolean)

(defconst br-min-width-window 25
  "*Minimum width of a browser class list window.
This together with the frame width determines the number of such windows.")

;;; ************************************************************************
;;; Public macros
;;; ************************************************************************

(if (fboundp 'window-highest-p)
    (defun br-listing-window-p ()
      "Is the selected window an OO-Browser listing window?"
      (if (br-in-browser) (window-highest-p (selected-window))))
  (defun br-listing-window-p ()
    "Is the selected window an OO-Browser listing window?"
    ;; Top of window is at top of frame.
    (if (br-in-browser)
	(= (nth 1 (window-edges)) br-top-of-frame))))

(defun br-non-listing-window-p ()
  "Is the selected window a non-OO-Browser listing window?"
  (not (br-listing-window-p)))

;;; ************************************************************************
;;; Public functions
;;; ************************************************************************

(defun br-browse ()
  "Internally invoke the OO-Browser, for browsing class hierarchies.
Use \\[br-help] and \\[br-help-ms] for help on browser usage."
  (interactive)
  (if (fboundp 'get-frame-for-mode)
      (let ((frame (get-frame-for-mode 'br-mode)))
	(if (frame-live-p frame)
	    (if (frame-visible-p frame)
		(select-frame frame)
	      (make-frame-visible frame)
	      (select-frame frame)))))
  ;; If not already in the browser, save window config.
  (if (br-in-browser)
      nil
    (let* ((env-build-process
	    (or (and (boundp 'compilation-in-progress)
		     (car compilation-in-progress))
		(get-buffer-process "*compilation*")))
	   (env-being-built
	    (and env-build-process
		 (boundp 'compile-command)
		 (stringp compile-command)
		 (string-match "oo-browser-env" compile-command)
		 (eq (process-status env-build-process) 'run)
		 (equal (getenv "OO_BROWSER_ENV") br-env-file))))
      (setq *br-prev-wconfig* (current-window-configuration)
	    br-in-browser (selected-frame))
      ;; If were previously in the browser, restore its saved window config,
      ;; otherwise, set up from scratch.
      (if (and *br-save-wconfig*
	       (br-set-window-configuration *br-save-wconfig*)
	       (br-in-browser))
	  nil
	(br-window-setup)
	(cond ((not env-being-built)
	       (br-version)
	       (message "Press {h} for help; use {C-c #} to see version and credits again.")
	       (br-show-all-classes)
	       (message "Press {h} for help; use {C-c #} to see version and credits again.")
	       ;; Wait for 60 seconds or until a single key sequence is given.
	       (sit-for 60)
	       (message ""))
	      (br-inhibit-version
	       (br-show-all-classes)))
	(if env-being-built nil	(br-help)))
      (if env-being-built
	  (let ((owind (selected-window)))
	    (br-to-view-window)
	    (switch-to-buffer "*compilation*")
	    (goto-char (point-max))
	    (select-window owind)
	    (message "Waiting for build to finish before loading Environment ...")))
      (run-hooks 'br-mode-hook
		 (intern (concat "br-" br-lang-prefix "mode-hook"))))))

;;;  ###autoload
;(defun br-add-class-file (&optional class-path lib-table-p save-file)
;  "Add a file of classes to the current Environment.
;Interactively or when optional CLASS-PATH is nil, CLASS-PATH defaults to the
;current buffer file pathname.  If optional LIB-TABLE-P is non-nil, add to
;Library Environment, otherwise add to System Environment.  If optional
;SAVE-FILE is t, the Environment is then stored to the filename given by
;`br-env-file'.  If SAVE-FILE is non-nil and not t, its string value is used
;as the file to which to save the Environment."
;  (interactive
;   (let ((class-path
;	  (expand-file-name
;	   (read-file-name (concat "Class file name to add"
;				   (if buffer-file-name
;				       (concat " (default \""
;					       (file-name-nondirectory
;						buffer-file-name)
;					       "\")"))
;				   ": ")
;			   nil buffer-file-name t)))
;	 (lib-table-match)
;	 (sys-table-match)
;	 (save-file))
;     (if (equal class-path "")
;	 nil
;       (setq lib-table-match
;	     (delq nil
;		   (mapcar
;		    (function
;		     (lambda (search-dir)
;		       (if (string-match (regexp-quote
;					  (expand-file-name search-dir))
;					 class-path) t)))
;		    br-lib-search-dirs)))
;       (if lib-table-match
;	   nil
;	 (setq sys-table-match
;	       (delq nil
;		     (mapcar
;		      (function
;		       (lambda (search-dir)
;			 (if (string-match (regexp-quote
;					    (expand-file-name search-dir))
;					   class-path) t)))
;		      br-sys-search-dirs)))))
;     (if (or lib-table-match sys-table-match)
;	 nil
;       (setq lib-table-match
;	     (y-or-n-p "Add to Library, rather than System part of Environment? ")))
;     (setq save-file (y-or-n-p
;		      (format "Save %s after this addition? " br-env-file)))
;     (list class-path lib-table-match save-file)))

;  (or class-path (setq class-path buffer-file-name))
;  (if (not (if class-path (file-readable-p class-path)))
;      (error "(br-add-class-file): %s is not readable" class-path))
;  (let* ((paths-parents-cons
;	   (let ((br-view-file-function 'br-insert-file-contents))
;	     (br-get-classes-from-source class-path)))
;	 (classes (car paths-parents-cons))
;	 (parents (cdr paths-parents-cons))
;	 (paths-key class-path)
;	 (path-htable (br-get-htable (if lib-table-p "lib-paths" "sys-paths")))
;	 (par-htable (br-get-htable
;		       (if lib-table-p "lib-parents" "sys-parents")))
;	 (child-htable (br-get-children-htable)))
;    (mapcar
;      (function
;	(lambda (class)
;	  (br-add-to-paths-htable class paths-key path-htable)))
;      classes)
;    (mapcar
;      (function
;	(lambda (parent-cons)
;	  (hash-add (car parent-cons) (cdr parent-cons) par-htable)))
;      parents)
;    (br-env-set-htables t)
;    (let ((child) (par-list) children)
;      (mapcar
;	(function
;	  (lambda (parent-cons)
;	    (setq child (cdr parent-cons)
;		  par-list (car parent-cons))
;	    (mapcar
;	      (function
;		(lambda (parent)
;		  (setq children (hash-get parent child-htable))
;		  (or (br-member child children)
;		      (hash-add (cons child children) parent child-htable))))
;	      par-list)))
;	parents)))
;  (cond ((eq save-file nil))
;	((eq save-file t) (br-env-save))
;	((br-env-save save-file))))

(defun br-ancestors (&optional arg features-string concrete-classes-flag)
  "Display ancestor tree whose root is the current class.
With optional prefix ARG, display all ancestor trees whose roots are in the
current listing.  With no ARG or if ARG = -1 or `br-invert-ancestors' is t,
the current class ancestry tree is inverted.  That is, it shows branches
going down towards the root class, so that parents appear above children.  If
ARG < -1 or `br-invert-ancestors' is t and ARG > 1, then the ancestry trees
of all classes in the current listing are inverted.

Optional second argument, FEATURES-STRING, is the plural name of the type of
features to display along with each ancestor class.

CONCRETE-CLASSES-FLAG non-nil means omit abstract classes from the tree."
  (interactive "p")
  (or arg (setq arg 1))
  (if br-invert-ancestors (setq arg (- arg)))
  (let* ((class-list
	  (if (and (/= arg 1) (/= arg -1))
	      (br-this-level-classes)
	    (br-find-class-name-as-list)))
	 (parents (delq nil (mapcar (function
				     (lambda (c) (br-get-parents c)))
				    class-list))))
    (cond ((and class-list
		(or parents
		    (and features-string
			 (if (/= 1 (length class-list))
			     t;; Assume some class will have features.
			   ;; This class must have features.
			   (br-list-features (car class-list))))))
	   (if (and (/= arg 1) (/= arg -1))
	       (message "Computing %s..."
			(or features-string "ancestors")))
	   (br-setup-next-window (if features-string
				     (substring features-string 0 1)
				   ;; ancestors
				   "a"))
	   (let (buffer-read-only)
	     (cond ((>= arg 0)
		    (br-ancestor-trees class-list nil nil
				       concrete-classes-flag))
		   (t
		    (br-ancestor-trees-inverted class-list nil nil
						concrete-classes-flag))))
	   (goto-char (point-min))
	   (if (and (/= arg 1) (/= arg -1))
	       (message "Computing %s...Done"
			(or features-string "ancestors")))
	   t)
	  ((null class-list)
	   (message "(OO-Browser):  Apply `br-%s' to a class."
		    (or features-string "ancestors"))
	   (beep))
	  (t
	   (message "No %s." (or features-string "ancestors"))
	   (beep)))))

(defun br-at (&optional arg)
  "Display the current class location in the inheritance graph.
The class is displayed among both its ancestors and descendants.
With optional prefix ARG, display the locations for all classes in the
current listing."
  (interactive "P")
  (let* ((parent)
	 (parent-list
	   (if arg
	       (br-this-level-classes)
	     (list (setq parent (br-find-class-name))))))
    (if arg (message "Computing class locations..."))
    (br-setup-next-window "@")
    (let (buffer-read-only)
      (br-descendant-trees (br-ancestor-roots parent-list))
      (goto-char (point-min))
      (if arg
	  (message "Computing class locations...Done")
	(re-search-forward (concat "\\(^\\|[ \t]+\\)" parent "$"))
	(goto-char (match-end 1))
	(recenter '(4))))))

(defun br-attributes (arg)
  "Display attributes of the current class (prefix ARG = 1) or of the current listing if ARG is other than 0 or 1.

With ARG = 0, the value of the variable, `br-inherited-features-flag', is
toggled and no other action is taken.

If `br-inherited-features-flag' is t, all attributes of each class are shown.
If nil, only lexically included attributes are shown and if the attributes of a
single class are requested and none are defined, the class definition is
displayed so that its attribute declarations may be browsed."
  (interactive "p")
  (cond ((and (integerp arg) (= arg 0))
	 (setq br-inherited-features-flag
	       (not br-inherited-features-flag))
	 (message "Inherited features/elements will %sbe shown."
		  (if br-inherited-features-flag "" "not ")))
	(br-inherited-features-flag
	 (br-inherited-attributes arg))
	(t (br-lexical-attributes arg))))

(defun br-categories (&optional arg)
  "Display categories directly associated with the current class.
This does not include any categories which the class inherits.
With optional prefix ARG, display categories of all classes in the current
listing."
  (interactive "P")
  (let ((has-categories)
	class-list categories class-and-categories)
    (setq class-list (cond (arg
			    (message "Computing class categories...")
			    (br-this-level-classes))
			   (t 
			    (br-find-class-name-as-list)))
	  categories
	  (delq nil (mapcar
		     (function
		      (lambda (class)
			(setq class-and-categories (br-list-categories class)
			      has-categories (or has-categories
						 class-and-categories))
			(cons class class-and-categories)))
		     class-list)))
    (cond ((not class-list)
	   (message "(OO-Browser):  Apply `br-categories' to a class.") (beep))
	  ((not has-categories)
	   (message "No class categories.") (beep))
	  (t
	   (br-setup-next-window "C")
	   (let (buffer-read-only done-set class)
	     (mapcar
	      (function
	       (lambda (class-and-categories)
		 (setq class (car class-and-categories))
		 (if (not (br-set-cons done-set class))
		     (insert class " ...\n")
		   ;; Class successfully added to set, so it has not been
		   ;; listed before.
		   (insert class "\n")
		   (br-insert-features (cdr class-and-categories) 2))))
	      categories))
	   (message "Computing class categories...Done")
	   (goto-char (point-min))
	   t))))

(defun br-children (&optional arg)
  "Display the children of the current class.
With optional prefix ARG, display the children of all the classes in the current
listing."
  (interactive "P")
  (let ((class-list (cond (arg
			   (message "Computing children...")
			   (br-this-level-classes))
			  (t
			   (br-find-class-name-as-list))))
	(has-children)
	children children-list)
    (setq children-list (delq nil (mapcar
				   (function
				    (lambda (parent)
				      (setq children
					    (br-get-children parent)
					    has-children
					    (or has-children children))
				      (cons parent children)))
				   class-list)))
    (cond ((not children-list)
	   (message "(OO-Browser):  Apply `br-children' to a class.")
	   (beep))
	  ((not has-children)
	   (message "No children.") (beep))
	  (t
	   (br-setup-next-window "c")
	   (let (buffer-read-only done-set parent)
	     (mapcar
	      (function
	       (lambda (parent-children-cons)
		 (setq parent (car parent-children-cons))
		 (if (not (br-set-cons done-set parent))
		     (insert parent " ...\n")
		   ;; Class successfully added to set, so it has not been
		   ;; listed before.
		   (insert parent "\n")
		   (br-insert-classes (cdr parent-children-cons) 2))))
	      children-list))
	   (if arg (message "Computing children...Done"))
	   (goto-char (point-min))
	   t))))

(defun br-class-stats (&optional prompt)
  "Display a statistics summary for current class.
Optional prefix arg PROMPT means prompt for a class name."
  (interactive "P")
  (let ((class-name (if prompt (br-complete-class-name) (br-find-class-name))))
    (if class-name
	(message "Class %s:  Parents: %d; Children: %d"
		 class-name (length (br-get-parents class-name))
		 (length (br-get-children class-name)))
      (error "No class name at point."))))

(defun br-cmd-help (key &optional full)
  "Show first line of doc for OO-Browser KEY in minibuffer.
With optional FULL, display full documentation for command."
  (interactive "kOO-Browser key binding: \nP")
  (let* ((cmd (let ((cmd (if (eq major-mode 'br-mode)
			     (lookup-key br-mode-map key)
			   (key-binding key))))
		(if (not (integerp cmd)) cmd)))
	 (doc (and cmd (documentation cmd)))
	 (end-line))
    (if doc
	(or full
	    (setq end-line (string-match "[\n]" doc)
		  doc (substitute-command-keys (substring doc 0 end-line))))
      (setq doc (format "No documentation for {%s} %s" key (or cmd ""))))
    (if (and cmd doc)
	(if full
	    (progn (br-to-view-window)
		   (other-window -1)
		   (describe-function cmd))
	  (message doc)))))

(defun br-count ()
  "Count the number of entries visible in current listing buffer.
Print the text result in the minibuffer when called interactively."
  (interactive)
  (let ((cnt (count-lines (point-min) (point-max))))
    (if (interactive-p)
	(message "%s contains %d entries." (buffer-name) cnt)
      cnt)))

(defun br-copyright ()
  "Display the OO-Browser copyright information in the viewer window."
  (interactive)
  (br-file-to-viewer "BR-COPY"))

(defun br-delete (&optional prompt)
  "Delete a class from the current Environment.
Does not alter descendency relations.
Optional prefix arg PROMPT means prompt for the class name."
  (interactive "P")
  (let ((class (if prompt (br-complete-class-name) (br-find-class-name))))
    (and class
	 (if (interactive-p)
	     (y-or-n-p (concat "Delete class " class " from Environment? "))
	   t)
	 (progn (br-real-delete-class class)
		;; Delete class name at point in listing window
		(or prompt (let (buffer-read-only)
			     (progn (beginning-of-line)
				    (delete-region
				     (point) (progn (forward-line 1)
						    (point))))))
		(message "Class " class " deleted.")))))

(defun br-descendants (&optional arg)
  "Display the descendant tree whose root is the current class.
With optional prefix ARG, display all descendant trees whose roots are
the classes in the current listing."
  (interactive "P")
  (let ((parent-list
	 (if arg
	     (br-this-level-classes)
	   (br-find-class-name-as-list))))
    (cond ((delq nil (mapcar
		      (function (lambda (parent)
				  (br-get-children parent)))
		      parent-list))
	   (if arg (message "Computing descendants..."))
	   (br-setup-next-window "d")
	   (let (buffer-read-only)
	     (br-descendant-trees parent-list))
	   (goto-char (point-min))
	   (if arg (message "Computing descendants...Done"))
	   t)
	  (t
	   (message "No descendants.") (beep)))))

(defun br-edit-entry (&optional prompt)
  "Edit the source code for any browser listing entry, such as a class or a feature.
Optional prefix arg PROMPT means prompt for the entry name; automatically
prompts if called interactively outside of a listing window, e.g. within a
source code buffer when the browser user interface is not displayed."
  (interactive "P")
  (let ((entry) (feature-tag))
    (if (or prompt (and (interactive-p) (not (br-in-browser))))
	(cond ((and (setq entry (br-complete-entry))
		    (string-match br-feature-signature-regexp entry))
	       (if (setq feature-tag (car (br-feature-tag-and-file entry)))
		   (br-feature nil feature-tag)
		 (error "(br-feature-tag-and-file): Could not find match for: `%s'" entry)))
	      (entry  ;; class name
		(br-edit nil entry))
	      (t (error "(br-complete-entry): Exited without selecting a match")))
      (cond ((br-at-feature-p)
	     (br-feature)
	     t)
	    ((and (setq entry (br-find-class-name))
		  (br-class-in-table-p entry))
	     (br-edit nil entry))
	    (t (error "(OO-Browser):  No `%s' entry in the current Environment"
		      entry))))))

(defun br-edit (&optional prompt class)
  "Edit a class in the viewer window.
Select viewer window.  With optional prefix arg PROMPT, prompt for class
name.  Optional CLASS is the one to edit.  Return t if class is displayed or
sent to an external viewer, else nil."
  (interactive "P")
  (or br-editor-cmd
      (not (br-in-browser))
      (br-in-view-window-p)
      (setq *br-prev-listing-window* (selected-window)))
  (br-view prompt t class))

(defun br-edit-ext (editor-cmd file line-num)
  "Invoke a non-standard EDITOR-CMD on FILE at LINE-NUM.
See also `br-editor-cmd'."
  (interactive "fFile to edit: ")
  (or editor-cmd (setq editor-cmd br-editor-cmd))
  (if (not (stringp editor-cmd))
      ;; must be a Lisp function that takes two args, a file and line number
      (funcall editor-cmd file line-num)
    (setq delete-exited-processes t)
    (let ((proc)
	  (name (concat br-ed-name (int-to-string br-ed-num))))
      (setq br-ed-num (1+ br-ed-num)
	    proc (br-edit-ext-start editor-cmd name file line-num))
      (if proc
	  (process-kill-without-query proc)
	(beep)
	(message "(OO-Browser):  Could not start external edit process: %s"
		 editor-cmd)))))

(defun br-editor-kill ()
  "Kill all current external editor sub-processes."
  (interactive)
  (if (br-kill-process-group br-ed-name br-ed-num "external editors")
      (setq br-ed-num 0)))

(defun br-entry-info ()
  "Display in the viewer window documentation for the current listing entry."
  (interactive)
  (if (fboundp 'br-insert-entry-info)

      ;; For languages which use the newer entry-info functions.
      (if (looking-at ".")
	  (progn
	    (message "Building entry info...")
	    (if (br-store-entry-info)
		(progn (message "Building entry info...Done")
		       (br-funcall-in-view-window
			(concat br-buffer-prefix-info "Info*")
			'br-insert-entry-info))
	      (beep)
	      (message "There is no documentation for `%s'."
		       (br-feature-name (br-feature-entry)))))
	(beep)
	(message "Move point to the beginning of an entry name line."))

    ;; For languages which use the older class-info functions.
    (let ((class-name (br-find-class-name)))
      (if class-name
	  (if (fboundp 'br-insert-class-info)
	      (progn
		(message "Building `%s' class info..." class-name)
		(if (br-store-class-info class-name)
		    (progn
		      (message "Building `%s' class info...Done" class-name)
		      (br-funcall-in-view-window
		       (concat br-buffer-prefix-info "Info*")
		       'br-insert-class-info))
		  (beep)
		  (message "There is no documentation for `%s'." class-name)))
	    (beep)
	    (message "No class information function for this language."))
	(beep)
	(message "No entry information function for this language.")))))

(defun br-exit-level (arg)
  "Return to prefix ARGth previous OO-Browser listing level.
The command is ignored with ARG < 1."
  (interactive "p")
  (setq arg (or arg 1))
  (let ((prev-wind-buf-line))
    (if (null *br-level-hist*)
	(and (> arg 0)
	     (message "No previous level to which to exit.")
	     (beep))
      (while (and (> arg 0) *br-level-hist*)
	(br-next-buffer
	 (int-to-string (br-listing-window-num)) br-buffer-prefix-blank)
	(setq prev-wind-buf-line (car *br-level-hist*)
	      *br-level-hist* (cdr *br-level-hist*)
	      arg (1- arg))
	(select-window (car prev-wind-buf-line))
	(switch-to-buffer (car (cdr prev-wind-buf-line))))
      (widen)
      ;; Position window lines exactly as before.
      (recenter (car (cdr (cdr prev-wind-buf-line)))))))

(defun br-feature (&optional view-only feature-tag)
  "Edit a feature in the viewer window.  Select viewer window.
Optional VIEW-ONLY non-nil means view rather than edit feature.
Optional FEATURE-TAG includes the signature of the feature to edit."
  (interactive)
  (or feature-tag
      ;; Get current feature tag
      (setq feature-tag (br-feature-get-tag)))
  (if (null feature-tag)
      (error "(br-feature): No definition for this entry")
    (let ((feature-sig
	   (if (br-feature-tag-p feature-tag)
	       (br-feature-tag-signature feature-tag)
	     feature-tag)))
      (cond ((and (or (and (not view-only) (br-edit-externally-p))
		      (and view-only (br-view-externally-p)))
		  (let* ((func (function
				(lambda (path unused)
				  (set-buffer (find-file-noselect path)))))
			 (br-edit-file-function func)
			 (br-view-file-function func)
			 (file (br-feature-tag-path feature-tag))
			 (line-num (save-excursion
				     (br-feature-found-p file feature-tag))))
		    (and line-num
			 (funcall (if view-only 'br-view-externally
				    'br-edit-externally)
				  file line-num)))))
	    ((progn
	       (br-to-view-window)
	       (br-feature-found-p (br-feature-tag-path feature-tag) feature-tag))
	     (if view-only
		 (progn (setq buffer-read-only t)
			(br-to-from-viewer))
	       (if (and buffer-file-name (file-writable-p buffer-file-name))
		   (setq buffer-read-only nil))))
	    ;;
	    ;; Feature not found.  Return to original window and signal an error.
	    (t (br-to-from-viewer)
	       (error "(br-feature): Cannot find definition of: `%s'" feature-sig))))))

(defun br-features (arg)
  "List features/elements of the current class (prefix ARG = 1) or of the current listing if ARG is other than 0 or 1.

With ARG = 0, the value of the variable, `br-inherited-features-flag', is
toggled and no other action is taken.

If `br-inherited-features-flag' is t, all features of each class are shown.
If nil, only lexically included features are shown and if the features of a
single class are requested and none are defined, the class definition is
displayed so that its feature declarations may be browsed."
  (interactive "p")
  (cond ((and (integerp arg) (= arg 0))
	 (setq br-inherited-features-flag
	       (not br-inherited-features-flag))
	 (message "Inherited features/elements will %sbe shown."
		  (if br-inherited-features-flag "" "not ")))
	(br-inherited-features-flag
	 (br-inherited-features arg))
	(t (br-lexical-features arg))))

;; Used outside of the browser user interface to display classes and features.
(defun br-find ()
  "Prompt with completion for a class or element name from the current Environment and display its definition for editing."
  (interactive)
  (br-edit-entry t))

(defun br-help (&optional file)
  "Display OO-Browser operation help information in the viewer window."
  (interactive)
  (or file (setq file "br-help"))
  (br-file-to-viewer file)
  (save-window-excursion
    (br-to-view-window)
    (br-mode)
    (use-local-map nil))
  (message ""))

(defun br-help-ms ()
  "Display OO-Browser mouse usage help information in the viewer window."
  (interactive)
  (br-help "br-help-ms"))

(defun br-implementors (&optional arg)
  "Display a list of classes which contain definitions for the current element name.
Ignore classes which inherit such definitions.  With optional prefix ARG,
display implementors of all elements within the current listing."
  (interactive "P")
  (let* ((interfaces-p (br-interface-support-p))
	 (categories-p (string-equal br-lang-prefix "objc-"))
	 (feature-regexp (concat "\\`" br-feature-type-regexp " "))
	 (class)
	 (tag)
	 (entries (if arg (br-this-level-entries)
		    (cond ((and (br-at-feature-p)
				(setq tag (br-feature-get-tag)
				      class (br-feature-tag-class tag))
				(not (br-member class '("[module]" "[package]"))))
			   (list (br-find-feature-entry)))
			  ((or (br-at-protocol-p) (br-at-class-category-p))
			   (br-find-class-name-as-list))
			  ;; ignore classes, packages and modules
			  (t nil)))))
    (if (or (null entries) (null (car entries)))
	(error
	 "(OO-Browser):  Apply `br-implementors' to a feature%s%s."
	 (if interfaces-p " or an interface/protocol" "")
	 (if categories-p " or a category" ""))
      (message "Computing implementors...")
      (br-setup-next-window "I")
      (let ((buffer-read-only) (implementor-tags) (classes)
	    (categories)
	    entry-category
	    class
	    start)
	(widen)
	(erase-buffer)
	(mapcar (function
		 (lambda (entry)
		   (cond
		    ;; features
		    ((string-match feature-regexp entry)
		     (setq implementor-tags
			   (br-feature-implementors
			    (br-feature-name entry)))
		     ;; Might contain invalid matches from a default class
		     ;; like [constant] (#define entry), so eliminate any
		     ;; such matches.
		     (if (br-member br-lang-prefix '("c++-" "java-" "objc-" "python-"))
			 (setq implementor-tags
			       (delq nil
				     (mapcar
				      (function
				       (lambda (tag)
					 (setq class (br-feature-tag-class tag))
					 (if (and (br-default-class-p
						   (br-feature-tag-class
						   tag))
						  (not (string-equal class "[function]")))
					     nil
					   tag)))
				      implementor-tags))))
		     ;; Sort tags
		     (setq implementor-tags
			   (br-feature-tag-sort-list implementor-tags))
		     ;; Get classes from tags
		     (setq classes (mapcar 'br-feature-tag-class
					   implementor-tags))
		     (if (equal (string-match br-feature-type-regexp entry) 0)
			 (insert (substring entry 0 2)
				 (br-feature-name entry)
				 "\n")
		       (insert entry "\n"))
		     (setq start (point))
		     (br-insert-classes classes 4)
		     (save-excursion
		       (goto-char start)
		       (br-feature-put-tags implementor-tags)))
		    ;;
		    ;; interfaces/protocols
		    ((and interfaces-p (eq (aref entry 0) ?\<))
		     (br-insert-protocol-implementors
		      (list entry) 0))
		    ;;
		    ;; Objective-C class categories
		    ((setq entry-category (br-class-category-p entry))
		     (if (null categories)
			 (setq categories
			       (mapcar 'br-feature-signature-to-name
				       (br-list-features
					objc-default-category-class))
			       ;; Set to t if there are no categories so that
			       ;; we don't recompute the set of categories
			       ;; for each entry.
			       categories (or categories t)))
		     (setq classes (objc-list-category-classes
				    entry-category categories))
		     (insert entry-category "\n")
		     (setq start (point))
		     (br-insert-classes classes 2))
		    ;;
		    ;; ignore other kinds of entries
		    (t))))
		entries))
      (goto-char 1)
      (message "Computing implementors...Done"))))

(defun br-info-language-specific ()
  "Display the OO-Browser manual section of specifics for the language of the current Environment."
  (interactive)
  (let ((lang-name (cdr (assoc br-lang-prefix br-env-lang-name-alist))))
    (if lang-name
	(id-info (concat "(oo-browser)"
			 lang-name " Specifics"))
      (error "(OO-Browser):  Invalid language prefix, `%s'" br-lang-prefix))))

(defun br-inherited-attributes (arg)
  "Display attributes declared within a class, including those from ancestors.
With optional prefix ARG, display attributes of all classes in the current
listing."
  (interactive "p")
  (let ((br-ancestor-function
	 (function
	  (lambda (class repeated-class indent)
	    (if repeated-class
		nil
	      (br-insert-features
	       (br-feature-list-attributes class) indent))))))
    (br-ancestors arg "attributes" nil)))

(defun br-inherited-features (arg)
  "Display class features, including those from ancestors.
With optional prefix ARG, display features of all classes in the current
listing."
  (interactive "p")
  (let ((br-ancestor-function
	 (function
	  (lambda (class repeated-class indent)
	    (if repeated-class
		nil
	      (br-insert-features (br-list-features class indent) indent))))))
    ;; Features of abstract classes are implemented by their
    ;; descendants so we don't want to show these feature
    ;; names a second time within the inheritance lattice.  The third
    ;; argument to this next call suppresses listing of inherited abstract
    ;; classes and their features except in cases where the class itself is
    ;; abstract.
    (br-ancestors arg "features" t)))

(defun br-inherited-routines (arg)
  "Display class routines, including those from ancestors.
With optional prefix ARG, display routines of all classes in the current
listing."
  (interactive "p")
  (let ((br-ancestor-function
	 (function
	  (lambda (class repeated-class indent)
	    (if repeated-class
		nil
	      (br-insert-features
	       (br-feature-list-routines class) indent))))))
    ;; Routines of abstract classes are implemented by their
    ;; descendants so we don't want to show these routine
    ;; names a second time within the inheritance lattice.  The third
    ;; argument to this next call suppresses listing of inherited abstract
    ;; classes and their routines except in cases where the class itself is
    ;; abstract.
    (br-ancestors arg "routines" t)))

(defun br-kill ()
  "Kill buffer in the viewer window and redisplay help text."
  (interactive)
  (br-do-in-view-window '(progn (kill-buffer nil) (br-help))))

(defun br-lexical-attributes (arg)
  "Display class attributes lexically defined within current class.
With numeric prefix ARG, display attributes of all classes in the current
listing."
  (interactive "p")
  (let ((has-attributes)
	class-list attribute-list class-and-attributes)
    (setq class-list (cond ((and (integerp arg) (/= arg 1))
			    (message "Computing class attributes...")
			    (br-this-level-classes))
			   (t 
			    (br-find-class-name-as-list)))
	  attribute-list
	  (delq nil (mapcar
		     (function
		      (lambda (class)
			(setq class-and-attributes
			      (br-feature-list-attributes class)
			      has-attributes (or has-attributes
					       class-and-attributes))
			(cons class class-and-attributes)))
		     class-list)))
    (cond ((not class-list)
	   (beep)
	   (message "(OO-Browser):  Apply `br-attributes' to a class."))
	  ((not has-attributes)
	   (message "No attributes declared for the class%s."
		    (if (= (length class-list) 1) "" "es"))
	   (beep))
	  (t
	   (br-add-level-hist)
	   (br-next-buffer)
	   (let (buffer-read-only done-set class)
	     (mapcar
	      (function
	       (lambda (class-and-attributes)
		 (setq class (car class-and-attributes))
		 (if (not (br-set-cons done-set class))
		     (insert class " ...\n")
		   ;; Class successfully added to set, so it has not been
		   ;; listed before.
		   (insert class "\n")
		   (br-insert-features (cdr class-and-attributes) 2))))
	      attribute-list)
	     (message "Computing class attributes...Done")
	     (goto-char (point-min)))))))

(defun br-lexical-features (arg)
  "Display class features lexically defined within current class.
With numeric prefix ARG, display features of all classes in the current
listing.

If the features of a single class are requested and there are no feature
definitions for the class, display the class definition so that its feature
declarations may be browsed."
  (interactive "p")
  (let ((has-features)
	class-list feature-list class-and-features)
    (setq class-list (cond ((and (integerp arg) (/= arg 1))
			    (message "Computing class features...")
			    (br-this-level-classes))
			   (t 
			    (br-find-class-name-as-list)))
	  feature-list
	  (delq nil (mapcar
		     (function
		      (lambda (class)
			(setq class-and-features (br-list-features class)
			      has-features (or has-features
					       class-and-features))
			(cons class class-and-features)))
		     class-list)))
    (cond ((not class-list)
	   (beep)
	   (message "(OO-Browser):  Apply `br-features' to a class."))
	  ((not has-features)
	   (if (and (= (length class-list) 1)
		    (br-class-path (car class-list)))
	       (if (br-view nil nil (car class-list))
		   (message
		    "No feature definitions, browse declarations instead."))
	     (message "No class features.") (beep)))
	  (t
	   (br-add-level-hist)
	   (br-next-buffer)
	   (let (buffer-read-only done-set class)
	     (mapcar
	      (function
	       (lambda (class-and-features)
		 (setq class (car class-and-features))
		 (if (not (br-set-cons done-set class))
		     (insert class " ...\n")
		   ;; Class successfully added to set, so it has not been
		   ;; listed before.
		   (insert class "\n")
		   (br-insert-features (cdr class-and-features) 2))))
	      feature-list)
	     (message "Computing class features...Done")
	     (goto-char (point-min)))))))

(defun br-lexical-routines (arg)
  "Display class routines lexically defined within current class.
With numeric prefix ARG, display routines of all classes in the current
listing.

If the routines of a single class are requested and there are no routine
definitions for the class, display the class definition so that its routine
declarations may be browsed."
  (interactive "p")
  (let ((has-routines)
	class-list routine-list class-and-routines)
    (setq class-list (cond ((and (integerp arg) (/= arg 1))
			    (message "Computing class routines...")
			    (br-this-level-classes))
			   (t 
			    (br-find-class-name-as-list)))
	  routine-list
	  (delq nil (mapcar
		     (function
		      (lambda (class)
			(setq class-and-routines
			      (br-feature-list-routines class)
			      has-routines (or has-routines
					       class-and-routines))
			(cons class class-and-routines)))
		     class-list)))
    (cond ((not class-list)
	   (beep)
	   (message "(OO-Browser):  Apply `br-routines' to a class."))
	  ((not has-routines)
	   (if (and (= (length class-list) 1)
		    (br-class-path (car class-list)))
	       (if (br-view nil nil (car class-list))
		   (message
		    "No routine definitions, browse declarations instead."))
	     (message "No class routines.") (beep)))
	  (t
	   (br-add-level-hist)
	   (br-next-buffer)
	   (let (buffer-read-only done-set class)
	     (mapcar
	      (function
	       (lambda (class-and-routines)
		 (setq class (car class-and-routines))
		 (if (not (br-set-cons done-set class))
		     (insert class " ...\n")
		   ;; Class successfully added to set, so it has not been
		   ;; listed before.
		   (insert class "\n")
		   (br-insert-features (cdr class-and-routines) 2))))
	      routine-list)
	     (message "Computing class routines...Done")
	     (goto-char (point-min)))))))

(defun br-lib-rebuild ()
  "Rescan Library components of the current Environment."
  (interactive)
  (if (call-interactively 'br-build-lib-htable)
      (br-show-all-classes)))

(defun br-lib-top-classes (&optional arg)
  "Display a list of the top-level Library classes.
With prefix ARG, display all Library classes."
  (interactive "P")
  (and (or (not (interactive-p))
	   (br-in-top-buffer-p)
	   (y-or-n-p "Exit to top-level class listing buffer? "))
       (cond (arg
	      (br-show-classes
	       (function (lambda () (br-all-classes "lib")))
	       nil t "Al")
	      (message "Listing of all Library classes"))
	     (t
	      (br-show-classes 'br-get-lib-top-classes t t "Tl")
	      (message "Listing of top-level Library classes")))
       (setq *br-level-hist* nil)))

(defun br-match (&optional expr arg again matched)
  "Show all class names in the current Environment that contain optional EXPR.
A nil value of EXPR means prompt for a value.  With optional prefix ARG, EXPR
is treated as a string.  By default, it is treated as a regular expression.
AGAIN non-nil shows the number of classes MATCHED from the last search,
allowing repeated narrowing of the search set.  An empty EXPR when AGAIN is
nil matches to all classes in the Environment."
  (interactive (list nil current-prefix-arg))
  (or expr (setq expr (read-string
		       (concat (if again (format "(%s matches)  " matched))
			       (if arg
				   "Find Environment class string matches"
				 "Find Environment class regular expression matches")
			       (if again " (RET to end): " ": ")))))
  (if (and again (equal expr ""))
      nil
    (let* ((match-expr (if arg (regexp-quote expr) expr))
	   (classes
	    (delq nil (mapcar
		       (function
			(lambda (cl)
			  (if (string-match match-expr cl) cl)))
		       (if again
			   (sort (br-this-level-classes) 'string-lessp)
			 (br-all-classes))))))
      (if classes
	  (progn (let (buffer-read-only)
		   (br-feature-clear-tags)
		   (erase-buffer)
		   (br-insert-classes classes 0))
		 (goto-char (point-min))
		 (br-match nil arg t (br-count)))
	(beep)
	(message "No matches for \"%s\"." expr)))))

(defun br-match-entries (&optional expr arg again matched)
  "Show all entries in the current listing that contain optional EXPR.
A nil value of EXPR means prompt for a value.  With optional prefix ARG, EXPR
is treated as a string.  By default, it is treated as a regular expression.
AGAIN non-nil means show the number of entries MATCHED from the last search,
allowing repeated narrowing of the search set.  An empty EXPR when AGAIN is
nil matches to all entries in the listing."
  (interactive (list nil current-prefix-arg))
  (or expr (setq expr (read-string
			(concat (if again (format "(%s matches)  " matched))
				(if arg
				    "Find string matches in listing"
				  "Find regular expression matches in listing")
				(if again " (RET to end): " ": ")))))
  (if (and again (equal expr ""))
      nil
    (let* ((match-expr (if arg (regexp-quote expr) expr))
	   (buffer-read-only))
      (goto-char (point-min))
      (if (not (re-search-forward match-expr nil t))
	  (progn (beep)
		 (message "No matches for \"%s\"." expr))
	(goto-char (point-min))
	(delete-non-matching-lines match-expr)
	(goto-char (point-min))
	(br-match-entries nil arg t (br-count))))))

(defun br-next-entry (arg)
  "Move point vertically down prefix ARG number of lines in a listing buffer."
  (interactive "p")
  (let ((end))
    (setq end (= (forward-line arg) arg))
    (and (looking-at "^$") (forward-line -1) (setq end t))
    (and end (message "No next entry.") (beep))))

(defun br-order (arg)
  "Order current browser listing window entries.
With prefix ARG other than 1 (the default), don't remove leading space from
entry lines before ordering.  Negative ARG means order in descending Ascii
sequence, otherwise order in ascending sequence."
  (interactive "p")
  (setq arg (or arg 1))
  (message "Ordering entries...")
  (let ((buffer-read-only)
	sort-args)
    (and (= arg 1) (progn (goto-char (point-min))
			  (while (re-search-forward "^[ \t]+" nil t)
			    (replace-match ""))))
    (if (string-lessp "19" emacs-version)
	(progn
	  ;; Emacs 19: This is slower than calling an external sort but it
	  ;; maintains the element tags in a listing, allowing further
	  ;; browsing from this buffer.
	  (sort-lines (< arg 0) (point-min) (point-max))
	  ;; Move [default] classes to the end of the sorted list.
	  (goto-char (point-min))
	  (if (re-search-forward "^[ \t]*\\[" nil t)
	      (let (start end)
		(beginning-of-line)
		(setq start (point))
		(goto-char (point-max))
		(re-search-backward "^[ \t]*\\[" nil t)
		(forward-line 1)
		(setq end (point))
		(goto-char (point-max))
		(append-to-buffer (current-buffer) start end)
		(delete-region start end))))
      ;;
      ;; Emacs 18: We can't maintain the buffer tags, so we just use a fast
      ;; external sort.
      (setq sort-args (list (point-min) (point-max) "sort" t t nil)
	    sort-args (if (< arg 0)
			  (if (stringp br-sort-options)
			      (nconc sort-args (list "-r" br-sort-options))
			    (nconc sort-args (list "-r")))
			(if (stringp br-sort-options)
			    (nconc sort-args (list br-sort-options))
			  sort-args)))
      (apply 'call-process-region sort-args)))
  (goto-char (point-min))
  (message "Ordering entries...Done"))

(defun br-parents (&optional arg)
  "Display the parents of the current class.
With optional prefix ARG, display parents of all the classes in the current
listing."
  (interactive "P")
  (let ((class-list (cond (arg
			   (message "Computing parents...")
			   (br-this-level-classes))
			  (t
			   (br-find-class-name-as-list))))
	(has-parents)
	parents parents-list)
    (setq parents-list
	  (delq nil (mapcar (function
			     (lambda (class)
			       (setq parents (br-get-parents class)
				     has-parents (or has-parents parents))
			       (cons class parents)))
			    class-list)))
    (cond ((not parents-list)
	   (message "(OO-Browser):  Apply `br-parents' to a class.") (beep))
	  ((not has-parents)
	   (message "No parents.") (beep))
	  (t
	   (br-setup-next-window "p")
	   (let (buffer-read-only done-set class)
	     (mapcar
	      (function
	       (lambda (class-parents-cons)
		 (setq class (car class-parents-cons))
		 (if (not (br-set-cons done-set class))
		     (insert class " ...\n")
		   ;; Class successfully added to set, so it has not been
		   ;; listed before.
		   (insert class "\n")
		   (br-insert-classes (cdr class-parents-cons) 2))))
	      parents-list))
	   (if arg (message "Computing parents...Done"))
	   (goto-char (point-min))
	   t))))

(defun br-prev-entry (arg)
  "Move point vertically up prefix ARG number of lines in a listing buffer."
  (interactive "p")
  (setq arg (- arg))
  (and (= (forward-line arg) arg)
       (message "No previous entry.")
       (beep)))

(defalias 'br-interfaces 'br-protocols)

(defun br-protocols (&optional arg)
  "Display the protocols to which the current class or protocol conforms, including inherited ones.

With optional prefix ARG (other than 0 or 1), display protocols of all
classes and protocols in the current listing.

With ARG = 0, the value of the variable, `br-protocols-with-classes-flag', is
toggled and no other action is taken."
  (interactive "P")
  (if (and (integerp arg) (= arg 0)
	   (br-protocol-support-p))
      (progn
	(setq br-protocols-with-classes-flag
	      (not br-protocols-with-classes-flag))
	(if br-protocols-with-classes-flag
	    (message
	     "Protocols/interfaces will %sbe included in initial class listings."
	     (if br-protocols-with-classes-flag "" "not "))))
    (let* ((protocols-p (br-protocol-support-p))
	   (class-list (if protocols-p
			   (cond (arg
				  (message "Computing all class protocols...")
				  (br-this-level-classes))
				 (t 
				  (br-find-class-name-as-list))))))
      (cond ((not protocols-p)
	     (beep)
	     (message "(OO-Browser):  No protocol browsing support for this language"))
	    ((not class-list)
	     (beep)
	     (message "(OO-Browser):  Apply `br-protocols' to a class."))
	    (t
	     (br-setup-next-window "P")
	     (let (buffer-read-only)
	       (br-ancestor-trees class-list))
	     (message "Computing all class protocols...Done")
	     (goto-char (point-min)))))))

(defun br-quit (&optional arg)
  "Quit the OO-Browser.
With optional prefix ARG, delete window configurations and listing
buffers associated with the browser."
  (interactive "P")
  (if (not (br-in-browser))
      (br-interrupt arg)
    (if (null arg)
	(setq *br-save-wconfig* (current-window-configuration))
      (if (featurep 'br-tree) (br-tree-kill))
      (br-viewer-kill)
      ;; Too dangerous to include (br-editor-kill) here.
      ;; The user can invoke it manually if desired.
      )
    ;; The following `let' clause is necessary since br-interrupt buries
    ;; buffers and so must be called before the window configuration restore,
    ;; but it also may set *br-prev-wconfig* to nil, so we have to cache its
    ;; value.
    (let ((wconfig *br-prev-wconfig*))
      (br-interrupt arg)
      (if wconfig (br-set-window-configuration wconfig)))
    (and (fboundp 'frame-name) (equal (frame-name) "OO-Browser")
	 (if arg (delete-frame) (make-frame-invisible)))
    nil)
  ;; Force menubar update under GNU Emacs.
  (if (fboundp 'set-menubar-dirty-flag)
      (set-menubar-dirty-flag)))

(defun br-refresh ()
  "Restore the OO-Browser to its state upon startup."
  (interactive)
  (br-window-setup)
  (br-show-all-classes)
  (br-help)
  (setq br-in-browser (selected-frame)))

(defun br-report-bug ()
  "Send a message to the OO-Browser discussion list."
  (interactive)
  (if (br-in-browser) (br-to-view-window))
  (hmail:compose "oo-browser-bugs@xemacs.org" '(hypb:configuration)))

(defun br-routines (arg)
  "Display routines of the current class (prefix ARG = 1) or of the current listing if ARG is other than 0 or 1.

With ARG = 0, the value of the variable, `br-inherited-features-flag', is
toggled and no other action is taken.

If `br-inherited-features-flag' is t, all routines of each class are shown.
If nil, only lexically included routines are shown and if the routines of a
single class are requested and none are defined, the class definition is
displayed so that its routine declarations may be browsed."
  (interactive "p")
  (cond ((and (integerp arg) (= arg 0))
	 (setq br-inherited-features-flag
	       (not br-inherited-features-flag))
	 (message "Inherited features/elements will %sbe shown."
		  (if br-inherited-features-flag "" "not ")))
	(br-inherited-features-flag
	 (br-inherited-routines arg))
	(t (br-lexical-routines arg))))

(defun br-set-window-configuration (wconfig)
  "Restore a window configuration, if possible, protecting against errors.
Return t if no error, nil otherwise."
  (condition-case ()
      (progn (set-window-configuration wconfig) t)
    (error nil)))

(defun br-sys-rebuild ()
  "Rescan System components of the current Environment."
  (interactive)
  (if (call-interactively 'br-build-sys-htable)
      (br-show-all-classes)))

(defun br-sys-top-classes (&optional arg)
  "Display list of top-level System classes.
With prefix ARG, display all System classes."
  (interactive "P")
  (and (or (not (interactive-p))
	   (br-in-top-buffer-p)
	   (y-or-n-p "Exit to top-level class listing buffer? "))
       (cond (arg
	      (br-show-classes
	       (function (lambda () (br-all-classes "sys")))
	       nil t "As")
	      (message "Listing of all System classes"))
	     (t
	      (br-show-classes 'br-get-sys-top-classes t t "Ts")
	      (message "Listing of top-level System classes")))
       (setq *br-level-hist* nil)))

;;;###autoload
(defun br-to-from-viewer ()
  "Move point to the viewer window or back to the last recorded listing window."
  (interactive)
  (if (br-in-view-window-p)
      (progn (if (and *br-prev-listing-window*
		      (if (fboundp 'window-live-p)
			  (window-live-p *br-prev-listing-window*)
			t))
		 (select-window *br-prev-listing-window*)
	       (other-window 1))
	     (setq *br-prev-listing-window* nil))
    (br-to-view-window)))

(defun br-toggle-c-tags ()
  "Toggle the value of the `br-c-tags-flag' flag."
  (interactive)
  (setq br-c-tags-flag (not br-c-tags-flag))
  (message "C constructs will %sbe added to C-based language Environments."
	   (if br-c-tags-flag "" "not ")))

(defun br-toggle-keep-viewed ()
  "Toggle the value of the `br-keep-viewed-classes' flag."
  (interactive)
  (setq br-keep-viewed-classes (not br-keep-viewed-classes))
  (message "Viewed buffers will no%s be kept after use."
	   (if br-keep-viewed-classes "w" "t")))

(defun br-show-all-classes ()
  "Display list of all Environment classes."
  (interactive)
  (br-show-top-classes t))

(defun br-show-top-classes (&optional arg)
  "Display list of top-level classes.
With prefix ARG, display all Environment classes."
  (interactive "P")
  (and (or (not (interactive-p))
	   (br-in-top-buffer-p)
	   (y-or-n-p "Exit to top-level class listing buffer? "))
       (cond (arg
	      (br-show-classes 'br-all-classes nil t "A")
	      (message "Listing of all Environment classes"))
	     (t
	      (br-show-classes 'br-get-top-classes t t "T")
	      (message "Listing of top-level classes")))
       (setq *br-level-hist* nil)))

(defun br-unique ()
  "Eliminate adjacent duplicate entry names from the current listing window.
If two adjacent entries look the same, one is eliminated, even if they refer
to different class elements."
  (interactive)
  (let ((buffer-read-only)
	(again t)
	first second)
    (goto-char (point-min))
    (setq first (br-feature-current))
    (while again
      (setq again (= (forward-line 1) 0)
	    second (br-feature-current))
      (if (not (string-equal first second))
	  (setq first second)
	(beginning-of-line)
	(delete-region (point) (progn (forward-line 1) (point)))
	;; back up to first line again
	(forward-line -1)))
    (goto-char (point-min))))

(defun br-version ()
  "Display the OO-Browser version number and credits."
  (interactive)
  (br-funcall-in-view-window
   (concat br-buffer-prefix-info "Help*")
   (function (lambda ()
	       (insert-file-contents (br-pathname "BR-VERSION"))
	       (hypb:display-file-with-logo)
	       (if (re-search-forward "<VERSION>" nil t)
		   (replace-match br-version t t))
	       (center-line)
	       (set-buffer-modified-p nil)))))

(defun br-view-entry (&optional prompt)
  "Display source for any browser listing entry.
Optional prefix arg PROMPT (if other than 0) means prompt for an entry name;
automatically prompts if called interactively outside of a listing window,
e.g. within a source code buffer when the browser user interface is not
displayed.  With a prefix arg of 0, the value of the variable,
`br-keep-viewed-classes', is toggled and no other action is taken."
  (interactive "P")
  (if (and (integerp prompt) (= prompt 0))
      (br-toggle-keep-viewed)
    (let ((entry) (feature-tag))
      (if (or prompt (and (interactive-p) (not (br-in-browser))))
	  (cond ((and (setq entry (br-complete-entry))
		      (string-match br-feature-signature-regexp entry))
		 (if (setq feature-tag (car (br-feature-tag-and-file entry)))
		     (br-feature 'view feature-tag)
		   (error "(br-feature-tag-and-file): Could not find match for: `%s'" entry)))
		(entry ;; class name
		 (br-view nil nil entry))
		(t (error "(br-complete-entry): Exited without selecting a match")))
	(cond ((br-at-feature-p)
	       (br-feature 'view)
	       t)
	      ((and (setq entry (br-find-class-name))
		    (br-class-in-table-p entry))
	       (br-view nil nil entry))
	      (t (error "(OO-Browser):  Entry may be referenced but not defined in the Environment.")))))))

(defun br-view (&optional prompt writable class)
  "Displays class file in viewer window.
Optional prefix arg PROMPT means prompt for class name.  Non-nil WRITABLE means
allow editing, otherwise display in read-only mode.  Non-nil CLASS is class to
display.  Return t if class is displayed or sent to an external viewer, else nil."
  (interactive "P")
  (or class (setq class (if prompt (br-complete-class-name)
			  (br-find-class-name))))
  (cond ((null class)
	 (beep)
	 (message "(OO-Browser):  Select a class to view.")
	 nil)
	((not (br-class-defined-p class)) nil)
	((and (or (and writable (br-edit-externally-p))
		  (and (not writable) (br-view-externally-p)))
	      (let* ((func (function
			    (lambda (path unused)
			      (set-buffer (find-file-noselect path)))))
		     (br-edit-file-function func)
		     (br-view-file-function func)
		     (line-num (save-excursion
				 (br-find-class class (not writable)))))
		(and line-num
		     (funcall (if writable 'br-edit-externally 'br-view-externally)
			      (br-class-path class) line-num)))))
	(t (let ((owind (selected-window))
		 viewer-obuf)
	     (unwind-protect
		 (progn (if (br-in-browser) (br-to-view-window))
			(setq viewer-obuf (current-buffer))
			(if (br-find-class class (not writable))
			    (progn 
			      (if (not (eq (current-buffer) viewer-obuf))
				  (save-excursion
				    (set-buffer viewer-obuf)
				    (if (and (not br-keep-viewed-classes)
					     buffer-read-only
					     (null (buffer-modified-p)))
					(kill-buffer (current-buffer)))))
			      t)))
	       (or writable (select-window owind)))))))

(defun br-view-ext (viewer-cmd file line-num)
  "Invoke a non-standard VIEWER-CMD on FILE at LINE-NUM.
See also `br-viewer-cmd'."
  (interactive "fFile to view: ")
  (or viewer-cmd (setq viewer-cmd br-viewer-cmd))
  (if (not (stringp viewer-cmd))
      ;; must be a Lisp function that takes two args, a file and line number
      (funcall viewer-cmd file line-num)
    (setq delete-exited-processes t)
    (let ((proc)
	  (name (concat br-vw-name (int-to-string br-vw-num))))
      (setq br-vw-num (1+ br-vw-num)
	    proc (br-view-ext-start viewer-cmd name file line-num))
      (if proc
	  (process-kill-without-query proc)
	(beep)
	(message "(OO-Browser):  Could not start external view process: %s"
		  viewer-cmd)))))

(defun br-view-full-frame ()
  "Delete all windows in the selected frame except for the viewer window."
  (interactive)
  (setq *br-save-wconfig* (current-window-configuration))
  (br-to-view-window)
  (let ((buf (current-buffer)))
    (br-interrupt)
    (delete-other-windows)
    (switch-to-buffer buf))
  (let* ((cmd (concat br-lang-prefix "browse"))
	 (key (car (where-is-internal (intern-soft cmd)))))
    (message "Recall OO-Browser with: {%s}"
	     (if key
		 (key-description key)
	       (concat (key-description
			(or (car (where-is-internal
				  'execute-extended-command))
			    "\M-x"))
		       " " cmd)))))

(defun br-viewer-scroll-down-by-line (arg)
  "Scroll the viewer window from within a listing window to show prefix ARG more prior lines (default is 1)."
  (interactive "p")
  (let ((owind (selected-window)))
    (unwind-protect
	(progn (br-to-view-window)
	       (scroll-down arg))
      (select-window owind))))

(defun br-viewer-scroll-up-by-line (arg)
  "Scroll the viewer window from within a listing window to show prefix ARG more following lines (default is 1)."
  (interactive "p")
  (let ((owind (selected-window)))
    (unwind-protect
	(progn (br-to-view-window)
	       (scroll-up arg))
      (select-window owind))))

(defun br-viewer-beginning-of-buffer ()
  "Scroll to the beginning of the viewer window buffer from within a listing window."
  (interactive)
  (let ((owind (selected-window)))
    (br-to-view-window)
    (beginning-of-buffer) ;; sets mark at prior location
    (select-window owind))
  (message "Beginning of buffer"))

(defun br-viewer-end-of-buffer ()
  "Scroll to the end of the viewer window buffer from within a listing window."
  (interactive)
  (let ((owind (selected-window)))
    (br-to-view-window)
    (end-of-buffer) ;; sets mark at prior location
    (select-window owind))
  (message "End of buffer"))

(defun br-viewer-kill ()
  "Kill all current external viewer sub-processes."
  (interactive)
  (if (br-kill-process-group br-vw-name br-vw-num "external viewers")
      (setq br-vw-num 0)))

(defun br-viewer-scroll-down (&optional arg)
  "Scroll the viewer window downward ARG lines or a windowful if no ARG."
  (interactive "P")
  (let ((owind (selected-window)))
    (unwind-protect
	(progn (br-to-view-window)
	       (scroll-down arg))
      (select-window owind))))

(defun br-viewer-scroll-up (&optional arg)
  "Scroll the viewer window upward ARG lines or a windowful if no ARG."
  (interactive "P")
  (let ((owind (selected-window)))
    (unwind-protect
	(progn (br-to-view-window)
	       (scroll-up arg))
      (select-window owind))))

(defun br-where (&optional prompt)
  "Display in the viewer window and return the full path of the defining file for a browser listing entry.
Optional prefix arg PROMPT means prompt for the entry name; automatically
prompts if called interactively outside of a listing window (in standalone
mode), e.g. within a source code buffer when the browser user interface is
not displayed.  If called in standalone mode with a prefix argument, the
command inserts the defining path at point rather than displaying it elsewhere." 
  (interactive "P")
  (let ((entry) (path) (tag)
	(standalone (and (interactive-p) (not (br-in-browser)))))
    (if (or prompt standalone)
	(cond ((and (setq entry (br-complete-entry))
		    (string-match br-feature-signature-regexp entry))
	       (setq path (cdr (br-feature-tag-and-file entry))))
	      (entry ;; class name
	       (setq path (br-class-defined-p entry)))
	      (t (error "(br-complete-entry): Exited without selecting a match")))
      (cond ((setq tag (br-feature-get-tag))
	     (setq path (br-feature-tag-path tag))
	     (if (equal br-lang-prefix "eif-")
		 (setq entry (br-feature-tag-name tag t))
	       (setq entry (br-feature-tag-signature tag))
	       (if (and entry (string-match "::" entry))
		   nil
		 (setq entry (concat (br-feature-tag-class tag) "::" entry)))
	       ;; Remove any trailing \{.
	       (if (string-match "\\s-*\{\\'" entry)
		   (setq entry (substring entry 0 (match-beginning 0))))))
	    ((setq entry (br-find-class-name))
	     (or (setq path (br-class-path entry))
		 (error "(OO-Browser):  No path for this class in current Environment")))
	    (t (error "(OO-Browser):  No entry for current line in current Environment"))))
    (cond ((null path) nil)
	  ((and prompt standalone) (insert path))
	  (t (let ((owind (selected-window))
		   (buf (buffer-name)))
	       (br-to-view-window)
	       (switch-to-buffer (get-buffer-create (concat buf "-Path")))
	       (setq buffer-read-only nil)
	       (buffer-disable-undo (current-buffer))
	       (erase-buffer)
	       (insert (format "`%s' is defined within\n    \"%s\"" entry path))
	       (br-major-mode)
	       (goto-char 1)
	       (select-window owind)
	       (message ""))))
    path))

(defun br-write-buffer (file)
  "Write the narrowed portion of the current browser buffer to a file."
  (interactive "FFile to write buffer to: ")
  (write-region (point-min) (point-max) file))

;;; ************************************************************************
;;; Private functions
;;; ************************************************************************

(defun br-add-level-hist ()
  ;; Even though this next line looks useless, it cures a problem with
  ;; window buffer correspondences when the OO-Browser is started, so don't
  ;; remove it.
  (set-buffer (window-buffer (selected-window)))
  (setq *br-level-hist*
	(cons (list (selected-window) (buffer-name) (br-wind-line-at-point))
	      *br-level-hist*)))

(defun br-ancestor-roots (class-list)
  "Return sorted list of CLASS-LIST's unique ancestors which do not inherit from any other class.
This list may include elements from CLASS-LIST itself."
  (let ((rtn) (parents) func)
    (setq func (function
		(lambda (class-list)
		  (mapcar
		   (function
		    (lambda (class)
		      (if (not (setq parents (br-get-parents class)))
			  (setq rtn (cons class rtn))
			(funcall func parents))))
		   class-list))))
    (funcall func class-list)
    (br-set-of-strings (sort rtn 'string-lessp))))

(defun br-ancestor-trees (class-list &optional depth offset concrete-classes-flag)
  "Insert ancestor trees starting with classes from CLASS-LIST.
Ancestor trees are not inverted, i.e. parents appear below children, not
above.  Indent each class in CLASS-LIST by optional DEPTH spaces (default is
0 in order to ensure proper initialization).  Offset each child level by
optional OFFSET spaces from its parent (which must be greater than zero,
default 2).  CONCRETE-CLASSES-FLAG non-nil means omit abstract classes from
the trees."
  (or offset (setq offset 2))
  (or depth (setq depth 0))
  (if (= depth 0) (setq br-tmp-class-set nil))
  (let ((prev-expansion-str " ...")
	parents expand-subtree)
    (mapcar
      (function
	(lambda (class)
	  (setq expand-subtree (br-set-cons br-tmp-class-set class)
		parents (if expand-subtree (br-get-parents class)))
	  (indent-to depth)
	  (insert class)
	  (and (not expand-subtree) (br-has-parents-p class)
	       (insert prev-expansion-str))
	  (insert "\n")
	  (if br-ancestor-function
	      (funcall br-ancestor-function
		       class (not expand-subtree) (+ depth offset)))
	  (if parents
	      (br-ancestor-trees parents (+ depth offset) offset
				 concrete-classes-flag))))
      (if (and concrete-classes-flag
	       ;; Always include the first level classes
	       (not (zerop depth)))
	  (delq nil (mapcar 'br-concrete-class-p class-list))
	class-list)))
  (if (zerop depth) (setq br-tmp-class-set nil)))

(defun br-ancestor-trees-inverted (class-list &optional depth offset concrete-classes-flag)
  "Insert ancestor trees starting with classes from CLASS-LIST.
Ancestor trees are inverted, parents appear above children as in other
browser listing windows.  Indent each class in CLASS-LIST by optional DEPTH
spaces (default is 0 in order to ensure proper initialization).  Offset each
child level by optional OFFSET spaces from its parent (which must be greater
than zero, default 2).  CONCRETE-CLASSES-FLAG non-nil means omit abstract
classes from the trees."
  (or offset (setq offset 2))
  (or depth (setq depth 0 br-tmp-depth 0))
  (if (= depth 0) (setq br-tmp-class-set nil))
  (let ((prev-expansion-str " ...")
	parents expand-subtree)
    (mapcar (function
	      (lambda (class)
		(setq expand-subtree (br-set-cons br-tmp-class-set class)
		      parents (if expand-subtree (br-get-parents class)))
		(if parents
		    (progn (setq br-tmp-depth
				 (max (+ depth offset) br-tmp-depth))
			   (br-ancestor-trees-inverted
			    parents (+ depth offset) offset
			    concrete-classes-flag)))
		(indent-to (- br-tmp-depth depth))
		(insert class)
		(and (not expand-subtree) (br-has-children-p class)
		     (insert prev-expansion-str))
		(insert "\n")
		(if br-ancestor-function
		    (funcall br-ancestor-function
			     class (not expand-subtree) (+ depth offset)))
		(if (= depth 0) (setq br-tmp-depth 0))))
	    (if (and concrete-classes-flag
		     ;; Always include the first level classes
		     (not (zerop depth)))
		(delq nil (mapcar 'br-concrete-class-p class-list))
	      class-list)))
  (if (zerop depth) (setq br-tmp-class-set nil)))

(defun br-at-class-category-p ()
  "Returns t iff point is on a class category listing line."
  (if (string-equal br-lang-prefix "objc-")
      (save-excursion (beginning-of-line) (looking-at ".*\("))))

(defun br-at-default-class-p ()
  "Returns t iff point is within a default class listing entry."
  (and (save-excursion
	 (beginning-of-line)
	 (looking-at "[ \t]*\\(\\[[^\]]+\\]\\)"))
       (>= (point) (match-beginning 1))
       (< (point) (match-end 1))))

(defun br-at-feature-p ()
  "Returns t iff point is on a feature listing line."
  ;; Sometimes as in the case of implementor entries, the listing entry
  ;; itself does not begin with a feature indicator character but the
  ;; entry from its associated tag does, so use that to test.
  (let* ((tag (br-feature-get-tag))
	 (entry (and tag (br-feature-tag-name tag nil t))))
    (and entry (equal (string-match br-feature-entry-regexp entry) 0))))

(defun br-at-protocol-p ()
  "Return non-nil if point is within a protocol listing entry line."
  (and (br-protocol-support-p)
       (save-excursion (beginning-of-line) (looking-at "[ \t]*<"))))

(defun br-browser-buffer-p (&optional buffer)
  "Returns t iff optional BUFFER or current buffer is an OO-Browser specific buffer."
  (equal 0 (string-match (concat br-buffer-prefix-inher
				 "\\|" br-buffer-prefix-blank
				 "\\|" (regexp-quote br-buffer-prefix-info))
			 (buffer-name buffer))))

(defun br-buffer-level ()
  "Returns current listing buffer level as a string."
  (let* ((name (buffer-name))
	 (pos (string-match "-[^-0-9]*\\([0-9]+\\)\\'" name)))
    (if pos (substring name (match-beginning 1) (match-end 1)))))

(defun br-class-level ()
  "Returns current class hierarchy level as an integer.
1 is the top level."
  (let ((level-string (br-buffer-level)))
    (if level-string (string-to-int level-string))))

(defun br-listing-window-num ()
  "Return listing window number, leftmost is 1, non-listing window = 0."
  (let ((wind (selected-window))
	(ctr 0))
    (br-to-view-window)
    (while (not (eq wind (selected-window)))
      (other-window 1)
      (setq ctr (1+ ctr)))
    ctr))

(defun br-cleanup ()
  "Cleanup and free browser Environment data structures."
  (let ((env-file (intern-soft (concat br-lang-prefix "env-file"))))
    (if env-file (set env-file nil)))
  (setq br-lang-prefix nil
	br-sys-paths-htable nil
	br-lib-paths-htable nil
	br-paths-htable nil
	br-sys-parents-htable nil
	br-lib-parents-htable nil
	br-parents-htable nil
	br-children-htable nil
	br-lib-prev-search-dirs nil
	br-sys-prev-search-dirs nil
	))

(defun br-clear (command-string)
  "Re-initialize all browser listing buffer displays.
COMMAND-STRING is a short mnemonic string to attach to the first listing
buffer name to help describe the listing command used."
  (let ((n (max 1 (/ (frame-width) br-min-width-window))))
    (br-to-view-window)
    (other-window 1)
    ;; This next expression resets the first level buffer name to be more
    ;; descriptive of what is being shown.
    (br-next-buffer (concat command-string "1"))
    (while (> n 1)
      (setq n (1- n))
      (br-next-buffer nil br-buffer-prefix-blank))
    (br-to-view-window)
    (other-window 1)))

(defun br-descendant-trees (class-list &optional indent offset)
  "Insert descendant trees starting with classes from CLASS-LIST.
Indent each class in CLASS-LIST by optional INDENT spaces (default is 0 in
order to ensure proper initialization).  Offset each child level by optional
OFFSET spaces from its parent (which must be greater than zero, default 2)."
  (or indent (setq indent 0))
  (or offset (setq offset 2))
  (if (= indent 0) (setq br-tmp-class-set nil))
  (let ((prev-expansion-str " ...")
	children expand-subtree)
    (mapcar (function
	      (lambda (class)
		(setq expand-subtree (br-set-cons br-tmp-class-set class)
		      children (if expand-subtree (br-get-children class)))
		(indent-to indent)
		(insert class)
		(and (not expand-subtree) (br-has-children-p class)
		     (insert prev-expansion-str))
		(insert "\n")
		(if children
		    (br-descendant-trees children (+ indent offset) offset))))
	    class-list))
  (if (= indent 0) (setq br-tmp-class-set nil)))

(defun br-display-buffer (suffix)
  "Displays browser buffer ending in SUFFIX in current window."
  (let ((buf (get-buffer (concat br-buffer-prefix suffix))))
    (if buf (progn (set-window-buffer (selected-window) buf)))
    buf))

(defun br-do-in-view-window (form)
  "Evaluate FORM in viewer window and then return to current window."
  (interactive)
  (let ((wind (selected-window)))
    (unwind-protect
	(progn (br-to-view-window)
	       (eval form))
      (select-window wind))))

(defun br-edit-ext-start (editor-cmd name file line-num)
  "Start an external viewer given by EDITOR-CMD using NAME applied to FILE at LINE-NUM."
  (apply 'start-process name name editor-cmd
	 (if (equal editor-cmd "xterm")
	     (nconc (list "-title" (if (stringp br-ed2)
				       (concat br-ed2 ": " file)
				     file))
		    (delq nil (list br-ed1 br-ed2
				    (if line-num (format "+%s" line-num))
				    br-ed3 br-ed4 br-ed5 br-ed6 br-ed7 br-ed8
				    br-ed9))
		    (list file))
	   (nconc (delq nil (list br-ed1 br-ed2
				  (if line-num (format "+%s" line-num))
				  br-ed3 br-ed4 br-ed5 br-ed6
				  br-ed7 br-ed8 br-ed9))
		  (list file)))))

(defun br-edit-externally-p ()
  (and br-editor-cmd (or hyperb:window-system
			 ;; Support custom Lisp-based edit commands on any
			 ;; display type.
			 (not (stringp br-editor-cmd)))))

(defun br-view-externally-p ()
  (and br-viewer-cmd (or hyperb:window-system
			 ;; Support custom Lisp-based view commands
			 ;; on any display type.
			 (not (stringp br-viewer-cmd)))))

(defun br-edit-externally (path line-num)
  "Displays PATH at LINE-NUM in a writable fashion using an external program.
If cannot display PATH, returns nil."
  (br-edit-ext br-editor-cmd path line-num))

(defun br-view-externally (path line-num)
  "Displays PATH at LINE-NUM in a read-only fashion using an external program and returns t.
If cannot display PATH, returns nil."
  (br-view-ext br-viewer-cmd path line-num))

(defun br-funcall-in-view-window (buffer function &optional no-erase)
  "Clear out BUFFER and display return value from invocation of FUNCTION in viewer window.
Move point to beginning of buffer and then return to current window.  BUFFER
may be a buffer name.
With optional NO-ERASE, buffer is not erased before function is called."
  (interactive)
  (let ((wind (selected-window)))
    (unwind-protect
	(progn (br-to-view-window)
	       (set-window-buffer (selected-window) (get-buffer-create buffer))
	       (let (buffer-read-only)
		 (if no-erase
		     (goto-char (point-min))
		   (erase-buffer))
		 (funcall function))
	       (goto-char (point-min)))
      (select-window wind))))

(defun br-file-to-viewer (filename)
  "Display FILENAME from OO-Browser source directory in browser viewer window.
FILENAME should not contain any path information."
  (br-funcall-in-view-window
   (concat br-buffer-prefix-info "Help*")
   (function (lambda ()
	       (insert-file-contents (br-pathname filename))
	       (set-buffer-modified-p nil)))))

(defun br-in-browser ()
  "Return selected frame if the OO-Browser is active in it, else return nil."
  (cond ((not (eq br-in-browser (selected-frame))) nil)
	((or (one-window-p 'nomini)
	     (and (fboundp 'window-list)
		  (< (length (window-list)) 3)))
	 (setq br-in-browser nil))
	(t br-in-browser)))


(defun br-in-top-buffer-p ()
  "Return t if point is in the top class listing buffer, else nil."
  (string-equal (br-buffer-level) "1"))

(defun br-in-view-window-p ()
  "Is point in a viewer window?
May return t even if not in the OO-Browser."
  (and (not (eq (selected-window) (minibuffer-window)))
       (br-non-listing-window-p)))

(defun br-init (env-file)
  "Initialization common to all OO-Browser invocations.  Uses ENV-FILE argument."
  (save-excursion (br-feature-tags-init env-file)))

(defun br-insert-classes (class-list &optional indent)
  "Insert CLASS-LIST in current buffer indented INDENT columns."
  (mapcar (function
	    (lambda (class-name)
	      (and indent (indent-to indent))
	      (and class-name (insert class-name "\n"))))
	  class-list))

(defun br-insert-protocol-implementors (protocol-list indent)
  (or indent (setq indent 0))
  (mapcar
   (function
    (lambda (item)
      (if (eq (aref item 0) ?\<)
	  ;; abstract class
	  (progn (if (zerop indent)
		     (progn (indent-to indent) (insert item "\n")))
		 (br-insert-protocol-implementors (br-get-children item)
						  (+ indent 2)))
	;; Don't recurse on non-abstract classes since their children
	;; simply inherit conformance to the protocol from them, so they
	;; do not add any information as implementors.
	(indent-to indent) (insert item "\n"))))
   protocol-list))

(defun br-interrupt (&optional arg)
  (if (null arg)
      (mapcar
       (function
	(lambda (buf)
	  (set-buffer buf)
	  (if (or (eq major-mode 'br-mode) (br-browser-buffer-p))
	      (bury-buffer nil))))
       (buffer-list))
    (setq *br-save-wconfig* nil
	  *br-prev-wconfig* nil
	  *br-prev-listing-window* nil)
    (mapcar
     (function
      (lambda (buf)
	(set-buffer buf)
	(if (or (eq major-mode 'br-mode)
		(br-browser-buffer-p))
	    (progn (let (buffer-read-only)
		     (br-feature-clear-tags)
		     (set-buffer-modified-p nil))
		   (kill-buffer (current-buffer))))))
     (buffer-list))
    (br-cleanup))
  (setq br-in-browser nil))

(defun br-mode ()
  "The major mode used by OO-Browser listing windows.
See the file \"br-help\" for browser usage information.
It provides the following keys: \\{br-mode-map}"
  (interactive)
  (use-local-map br-mode-map)
  (setq major-mode 'br-mode)
  (setq mode-name "OO-Browse")
  (set-syntax-table text-mode-syntax-table)
  (setq local-abbrev-table text-mode-abbrev-table)
  (setq case-fold-search t)
  (setq buffer-read-only t)
  (if (fboundp 'popup-mode-menu)
      (setq mode-popup-menu id-popup-br-menu))
  (run-hooks 'br-class-list-hook)
  (run-hooks 'br-mode-hook))

(defun br-next-buffer (&optional special alt-prefix)
  "Returns next sequential browser buffer or special one if optional SPECIAL is non-nil.
Non-nil ALT-PREFIX is used as prefix in buffer name."
  (let* ((suffix (concat special
			 (if (and (stringp special)
				  (string-match "[0-9]\\'" special))
			     nil
			   (int-to-string (1+ (or (br-class-level) 0))))))
	 (buf (get-buffer-create
	       (concat (or alt-prefix br-buffer-prefix)
		       (if (integerp suffix)
			   (int-to-string suffix)
			 suffix)))))
    (if buf (progn
	      (or special (br-next-listing-window))
	      (set-window-buffer (selected-window) buf)
	      (let (buffer-read-only)
		(kill-all-local-variables)
		(buffer-disable-undo (current-buffer))
		;; Clear out any feature tags that may have been associated
		;; with this buffer, so we don't mistakenly reference them.
		(br-feature-clear-tags)
		(erase-buffer))
	      (make-local-variable 'frame-title-format)
	      (setq frame-title-format
		    (list "OO-Browser-" br-version "@" (system-name) ":  "
			  br-env-name " - " br-env-file))
	      (br-mode)
	      (br-set-mode-line)
	      (set-buffer-modified-p nil)))
    buf))

(defun br-next-listing-window (&optional prev)
  "Move to next browser listing window (non-viewer window).
Optional PREV means to previous window."
  ;; The `br-non-listing-window-p' call below calls `br-in-browser' which if
  ;; called from `br-window-setup' when initializing browser windows can
  ;; set the variable, br-in-browser, to nil since it doesn't recognize the
  ;; window configuration.  Prevent this by making the variable local for this
  ;; function.
  (let ((owind (selected-window))
	(br-in-browser br-in-browser))
    (while (progn (other-window (if prev -1 1))
		  (if (br-non-listing-window-p)
		      (not (eq (selected-window) owind))))
      (setq br-in-browser (selected-frame)))))

(defun br-pathname (filename)
  "Return full pathname for FILENAME in browser Elisp directory."
  (if br-directory
      (expand-file-name filename br-directory)
    (error "The `br-directory' variable must be set to a string value.")))

(defun br-resize (min-width)
  "Resize browser listing windows to have MIN-WIDTH."
  (interactive)
  (let* ((window-min-width 3)
	 (oldn (1- (length (br-window-list))))
	 (n (max 1 (/ (frame-width) min-width)))
	 (numw n)
	 (diff (- numw oldn))
	 (width (/ (frame-width) numw))
	 (obuf (current-buffer)))
    (br-to-first-list-window)
    (cond ((= diff 0)
	   (br-resize-windows numw width))
	  ((> diff 0)
	   (setq n oldn)
	   (while (> n 1)
	     (setq n (1- n))
	     (shrink-window-horizontally (max 0 (- (window-width)
						   min-width)))
	     (br-next-listing-window))
	   (setq n diff)
	   (while (> n 0)
	     (setq n (1- n))
	     (split-window-horizontally (max window-min-width
					     (- (window-width)
						min-width)))) 
	   (setq n oldn)
	   (while (< n numw)
	     (setq n (1+ n))
	     (br-next-listing-window)
	     (br-next-buffer n br-buffer-prefix-blank))
	   (br-to-first-list-window)
	   (br-resize-windows numw width)
	   )
	  (t  ;; (< diff 0)
	   (while (> n 0)
	     (setq n (1- n))
	     (br-next-listing-window))
	   (setq n (- diff))
	   (while (> n 0)
	     (setq n (1- n))
	     (delete-window))
	   (br-to-first-list-window)
	   (br-resize-windows numw width)
	   ))
    (setq br-min-width-window min-width)
    (let ((owind (get-buffer-window obuf)))
      (if owind
	  (select-window owind)
	(br-to-view-window)
	(br-next-listing-window)))))

(defun br-resize-narrow ()
  "Narrow listing windows by 10 characters."
  (interactive)
  (if (<= window-min-width (- br-min-width-window 10))
      (br-resize (max window-min-width (- br-min-width-window 10)))
    (beep)))

(defun br-resize-widen ()
  "Widen listing windows by 10 characters."
  (interactive)
  (if (and (>= (frame-width) (+ br-min-width-window 10))
	   (> (length (br-window-list)) 2))
      (br-resize (min (frame-width) (+ br-min-width-window 10)))
    (beep)))

(defun br-resize-windows (n width)
  (while (> n 1)
    (setq n (1- n))
    (shrink-window-horizontally (- (window-width) width))
    (br-next-listing-window)))

(defun br-set-mode-line ()
  "Set mode line string."
  (setq mode-line-format (list "  %17b --" '(-3 . "%p") "-%-")
	mode-line-buffer-identification (list (buffer-name)))
  (set-buffer-modified-p t))

(defun br-setup-next-window (command-string)
  "Setup to display OO-Browser command output in the next listing window.
COMMAND-STRING is usually a one character mnemonic string for the command
generating the output for the window.  It is added to the buffer name
preceding a level number to aid the user in navigation."
  (let ((next-level
	 (if (and (stringp command-string)
		  (string-match "[0-9]\\'" command-string))
	     nil
	   (int-to-string (1+ (or (br-class-level) 0))))))
    (br-add-level-hist)
    (br-next-listing-window)
    (br-next-buffer (concat command-string next-level))))

(defun br-show-classes (func top-only-flag &optional uniq command-string)
  "Display a list of classes generated by calling FUNC.
TOP-ONLY-FLAG means only top-level classes (those that don't inherit from any
other non-abstract class) are listed.  Optional UNIQ means sort and eliminate
duplicates.
COMMAND-STRING is a short mnemonic string to attach to the listing buffer name
to help describe the listing command used."
  (message "Ordering classes...")
  (let ((classes (funcall func)))
    (setq classes (br-class-list-filter classes top-only-flag))
    (br-clear command-string)
    (let (buffer-read-only)
      (erase-buffer)
      (br-insert-classes classes)
      (if uniq
	  (progn
	    (if (stringp br-sort-options)
		(call-process-region (point-min) (point-max) "sort" t t nil
				     br-sort-options)
	      (call-process-region (point-min) (point-max) "sort" t t nil))
	    (if (or (and (stringp br-sort-options)
			 (string-match "u" br-sort-options))
		    ;; Then sort made the list of elements unique, so
		    ;; do nothing, or if can't find uniq program, do
		    ;; nothing.
		    (not (locate-file "uniq" exec-path ":.exe")))
		nil
	      (call-process-region (point-min) (point-max) "uniq" t t))))))
  (goto-char (point-min))
  (message "Ordering classes...Done"))

(defun br-this-level-classes (&optional keep-indent)
  "Return the class entries from the current browser listing.
Optional KEEP-INDENT non-nil means keep indentation preceding class name."
  (let ((classes)
	(feature-regexp (format "^[ \t]*%s " br-feature-type-regexp)))
    (save-excursion
      (goto-char (point-min))
      (while (and (not (looking-at "^[ \t]*$"))
		  (if (and
		       ;; Treat protocol/interface and category entries as classes.
		       (not (looking-at "^[ \t]*[<\(]"))
		       ;; Ignore feature entries
		       (looking-at feature-regexp))
		      t ;; skip this entry
		    ;; assume is a class
		    (setq classes (cons (br-find-class-name keep-indent)
					classes)))
		  (= (forward-line 1) 0))))
    (nreverse (delq nil classes))))

(defun br-this-level-entries ()
  "Return list of all entries in the current listing."
  (let ((entries)
	(feature-regexp (format "^[ \t]*%s " br-feature-type-regexp)))
    (save-excursion
      (goto-char (point-min))
      (while (and (not (looking-at "^[ \t]*$"))
		  (if (looking-at feature-regexp)
		      ;; a feature
		      (setq entries (cons (br-find-feature-entry) entries))
		    ;; assume is a class
		    (setq entries (cons (br-find-class-name) entries)))
		  (= (forward-line 1) 0))))
    (nreverse (delq nil entries))))

(defun br-this-level-features ()
  "Return list of features in the current listing."
  (let ((feature-regexp (concat "[ \t]*" br-feature-entry-regexp))
	(feature-list))
    (save-excursion
      (goto-char (point-min))
      (while (progn (if (looking-at feature-regexp)
			(setq feature-list
			      (cons (br-find-feature-entry) feature-list)))
		    (= (forward-line 1) 0))))
    (nreverse (delq nil feature-list))))

(defun br-to-first-list-window ()
  (br-to-view-window)
  (br-next-listing-window))

(defun br-to-tree ()
  "If point is within ellipses (...), move to the inheritance expansion for the current class."
  (if (save-excursion
	(skip-chars-backward ".")
	(looking-at "\\.\\.\\."))
      (progn (beginning-of-line)
	     (let ((class-expr (concat "^[ \t]*"
				       (br-find-class-name)
				       "$")))
	       (if (re-search-backward class-expr nil t)
		   (progn (skip-chars-forward " \t")
			  (recenter '(4))
			  t))))))

(defun br-to-view-window ()
  "Move to viewer window."
  (if (br-in-view-window-p)
      nil
    (setq *br-prev-listing-window* (selected-window))
    (while (and (not (br-in-view-window-p))
		(progn (other-window 1)
		       (not (eq (selected-window)
				*br-prev-listing-window*)))))))

(defun br-window-setup ()
  (and (fboundp 'modify-frame-parameters)
       (cdr (assq 'unsplittable (frame-parameters)))
       (modify-frame-parameters (selected-frame) '((unsplittable))))
  (delete-other-windows)
  ;; Set top of frame line in case it is not 0.
  (or (fboundp 'window-highest-p)
      (setq br-top-of-frame (nth 1 (window-edges))))
  (split-window-vertically nil)
  (let* ((n (max 1 (/ (frame-width) br-min-width-window)))
	 (width (/ (frame-width) n))
	 (start-win))
    ;; `A' means all classes will be listed, 1 = first buffer
    (br-next-buffer "A1")
    (while (> n 1)
      (setq n (1- n))
      (if (<= (window-width (selected-window)) width)
	  (progn (setq start-win (selected-window))
		 (while (and (progn (other-window 1)
				    (if (fboundp 'window-highest-p)
					(window-highest-p (selected-window))
				      (= (nth 1 (window-edges))
					 br-top-of-frame)))
			     (not (eq (selected-window) start-win))
			     (<= (window-width (selected-window)) width)))))
      (split-window-horizontally width)
      (br-next-buffer nil br-buffer-prefix-blank)))
  ;;
  ;; Leave point in the first window
  (br-to-view-window)
  (other-window 1))

(defun br-view-ext-start (viewer-cmd name file line-num)
  "Start an external viewer given by VIEWER-CMD using NAME applied to FILE at LINE-NUM."
  (apply 'start-process name name viewer-cmd
	 (if (equal viewer-cmd "xterm")
	     (nconc (list "-title" (if (stringp br-vw2)
				       (concat br-vw2 ": " file)
				     file))
		    (delq nil (list br-vw1 br-vw2 
				    (if line-num (format "+%s" line-num))
				    br-vw3 br-vw4 br-vw5 br-vw6 br-vw7 br-vw8
				    br-vw9))
		    (list file))
	   (nconc (if line-num (list (format "+%s" line-num)))
		  (delq nil (list br-vw1 br-vw2 br-vw3 br-vw4 br-vw5 br-vw6
				  br-vw7 br-vw8 br-vw9))
		  (list file)))))

;;; ************************************************************************
;;; Private variables
;;; ************************************************************************

(defvar br-ancestor-function nil
  "If non-nil, a function of 3 arguments called after each ancestor class is inserted into an ancestry listing.
First argument is the class just inserted, second argument is a flag
indicating whether class has previously been displayed within the listing and
third argument is the number of spaces to indent each feature entry for this
class.")

(defvar br-top-of-frame 0
  "Frame-relative line number at which the OO-Browser frame's uppermost windows start.")

(defvar br-ed-num 0)
(defvar br-ed-name "extEd")
(defvar br-vw-num 0)
(defvar br-vw-name "extVw")

(defvar br-in-browser nil
  "Equal to the frame displaying the OO-Browser when in use, else nil.")

(defvar br-lib-search-dirs nil
  "List of directories below which OO source files and other library
directories are found.  A library is a stable group of OO classes.  Do not
set this variable directly.  Each OO language library which invokes
`br-browse' should set it.")

(defvar br-sys-search-dirs nil
  "List of directories below which OO source files and other system
directories are found.  A system is a group of OO classes that are likely to
change.  Do not set this variable directly.  Each OO language library which
invokes `br-browse' should set it.")

(defvar *br-level-hist* nil
  "Internal history of visited listing windows and buffers.")

(defvar *br-prev-listing-window* nil
  "Saves listing window used prior to viewer window entry.
Allows return to previous listing window when done with the viewer.")

(defvar *br-prev-wconfig* nil
  "Saves window configuration prior to browser entry.")

(defvar *br-save-wconfig* nil
  "Saves window configuration between invocations of the browser.")

(defconst br-buffer-prefix-inher "OO-Browse-")
(defconst br-buffer-prefix-blank "Blank-")
(defconst br-buffer-prefix-info "*OO-Browser ")
(defvar br-buffer-prefix br-buffer-prefix-inher
  "Browser buffer name prefix.")


(defvar br-mode-map nil
  "Keymap containing OO-Browser commands.")
(if br-mode-map
    nil
  (setq br-mode-map (make-keymap))
  (suppress-keymap br-mode-map)
  (define-key br-mode-map "@"        'br-at)
  (define-key br-mode-map "="        'br-attributes)
  (define-key br-mode-map "\<"       'br-viewer-beginning-of-buffer)
  (define-key br-mode-map "\>"       'br-viewer-end-of-buffer)
  (define-key br-mode-map ","        'br-viewer-scroll-down-by-line)
  (define-key br-mode-map "."        'br-viewer-scroll-up-by-line)
  (define-key br-mode-map "1"        'br-view-full-frame)
;  (define-key br-mode-map "\C-c^"    'br-add-class-file)
  (define-key br-mode-map "a"        'br-ancestors)
  (define-key br-mode-map "A"        'br-show-all-classes)
  (define-key br-mode-map "\M-a"     'br-name-add)
  (define-key br-mode-map "b"        'br-buffer-menu)
  (define-key br-mode-map "\C-c\C-b" 'br-report-bug)
  (define-key br-mode-map "c"        'br-children)
  (define-key br-mode-map "C"        'br-categories)
  (define-key br-mode-map "\M-c"     'br-class-stats)
  (define-key br-mode-map "\C-c\C-c" 'br-env-browse)
  (define-key br-mode-map "d"        'br-descendants)
  (define-key br-mode-map "\C-c\C-d" 'br-delete)
  ;; {M-d} is used down below for `br-tree'
  (define-key br-mode-map "e"        'br-edit-entry)
  (define-key br-mode-map "\M-e"     'br-env-stats)
  (define-key br-mode-map "\C-c\C-e" 'br-env-rebuild)
  (define-key br-mode-map "f"        'br-features)
  (define-key br-mode-map "F"        'br-feature-signature)
  ;; {M-f} is used down below for `br-tree-features-toggle'
  ;; {M-g} is used down below for `br-tree-graph'
  (define-key br-mode-map "?"        'br-help)
  (define-key br-mode-map "h"        'br-help)
  (define-key br-mode-map "H"        'br-help-ms) ;; mouse help
  (define-key br-mode-map "i"        'br-entry-info)
  (define-key br-mode-map "I"        'br-implementors)
  (define-key br-mode-map "j"        'br-feature-view-declaration)
  (define-key br-mode-map "J"        'br-feature-edit-declaration)
  (define-key br-mode-map "\C-c\C-k" 'br-kill)
  ;; {M-k} is used down below for `br-tree-kill'
  (define-key br-mode-map "l"        'br-lib-top-classes)
  (define-key br-mode-map "L"        'br-lib-rebuild)
  (define-key br-mode-map "\M-l"     'br-names-display)
  (define-key br-mode-map "\C-c\C-l" 'br-env-load)
  (define-key br-mode-map "m"        'br-match)
  (define-key br-mode-map "M"        'br-match-entries)
  (define-key br-mode-map "\M-m"     'br-name-remove)
  ;; "\C-c\C-m" is reserved for future use.
  (define-key br-mode-map "\M-n"     'br-name-change)
  (define-key br-mode-map "\C-n"     'br-next-entry)
  (define-key br-mode-map "o"        'br-order)
  (define-key br-mode-map "p"        'br-parents)
  (define-key br-mode-map "P"        'br-protocols)
  (define-key br-mode-map "\C-p"     'br-prev-entry)
  (define-key br-mode-map "q"        'br-quit)
  (define-key br-mode-map "r"        'br-routines)
  (define-key br-mode-map "\M-r"     'br-name-replace)
  (define-key br-mode-map "\C-c\C-r" 'br-refresh)
  (define-key br-mode-map "s"        'br-sys-top-classes)
  (define-key br-mode-map "S"        'br-sys-rebuild)
  (define-key br-mode-map "\C-c\C-s" 'br-env-save)
  (define-key br-mode-map "t"        'br-show-top-classes)
  (define-key br-mode-map "T"        'br-show-top-classes)
  (define-key br-mode-map "u"        'br-unique)
  (define-key br-mode-map "v"        'br-view-entry)
  (define-key br-mode-map "V"        'br-view-friend)
  (define-key br-mode-map "\C-c\C-v" 'br-to-from-viewer)
  (define-key br-mode-map "\C-c\C-w" 'br-write-buffer)
  (define-key br-mode-map "w"        'br-where)
  (define-key br-mode-map "x"        'br-exit-level)
  (define-key br-mode-map "\C-x-"    'br-resize-narrow)
  (define-key br-mode-map "\C-x+"    'br-resize-widen)
  (define-key br-mode-map "#"        'br-count)
  (define-key br-mode-map "\C-c#"    'br-version)
  (define-key br-mode-map " "        'br-viewer-scroll-up)
  (define-key br-mode-map "\177"     'br-viewer-scroll-down)
  (if (string-match "XEmacs" emacs-version)
      (define-key br-mode-map '[backspace] 'br-viewer-scroll-down))
  ;;
  ;; Mouse keys
  (cond ((fboundp 'popup-mode-menu) nil)
	(hyperb:xemacs-p
	 (define-key br-mode-map 'button3 'br-popup-menu)
	 (define-key br-mode-map 'button3up nil))
	(t ;; hyperb:emacs19-p
	 (define-key br-mode-map [down-mouse-3] 'br-popup-menu)
	 (define-key br-mode-map [mouse-3] nil)))
  ;;
  ;; Define graphical browser keys if a window system is available.
  (if hyperb:window-system
      (progn (require 'br-tree)
	     (define-key br-mode-map "\M-d" 'br-tree)
	     (define-key br-mode-map "\M-f" 'br-tree-features-toggle)
	     (define-key br-mode-map "\M-g" 'br-tree-graph)
	     (define-key br-mode-map "\M-k" 'br-tree-kill))))

(defvar br-tmp-class-set nil
  "Set of classes created for temporary use by br-*-trees functions.")
(defvar br-tmp-depth 0
  "Temporary variable indicating inheritance depth of class in `br-ancestor-trees-inverted'.")

(provide 'br)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.