summaryrefslogtreecommitdiff
path: root/lisp/progmodes/ebrowse.el
blob: bdc4c57631f5b95ce07ca2a3a42cc5ca0421625c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
;;; ebrowse.el --- Emacs C++ class browser & tags facility

;; Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
;; 2002, 2003, 2004, 2005, 2006, 2007, 2008
;; Free Software Foundation Inc.

;; Author: Gerd Moellmann <gerd@gnu.org>
;; Maintainer: FSF
;; Keywords: C++ tags tools

;; This file is part of GNU Emacs.

;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:

;; This package implements

;; - A class browser for C++
;; - A complete set of tags-like functions working on class trees
;; - An electric buffer list showing class browser buffers only

;; Documentation is found in a separate Info file.

;;; Code:

(require 'easymenu)
(require 'view)
(require 'ebuff-menu)

(eval-when-compile
  (require 'cl)
  (require 'helper))


;;; User-options

(defgroup ebrowse nil
  "Settings for the C++ class browser."
  :group 'tools)


(defcustom ebrowse-search-path nil
  "*List of directories to search for source files in a class tree.
Elements should be directory names; nil as an element means to try
to find source files relative to the location of the BROWSE file loaded."
  :group 'ebrowse
  :type '(repeat (choice (const :tag "Default" nil)
                         (string :tag "Directory"))))


(defcustom ebrowse-view/find-hook nil
  "*Hooks run after finding or viewing a member or class."
  :group 'ebrowse
  :type 'hook)


(defcustom ebrowse-not-found-hook nil
  "*Hooks run when finding or viewing a member or class was not successful."
  :group 'ebrowse
  :type 'hook)


(defcustom ebrowse-electric-list-mode-hook nil
  "*Hook called by `ebrowse-electric-position-mode'."
  :group 'ebrowse
  :type 'hook)


(defcustom ebrowse-max-positions 50
  "*Number of markers saved on electric position stack."
  :group 'ebrowse
  :type 'integer)



(defgroup ebrowse-tree nil
  "Settings for class tree buffers."
  :group 'ebrowse)


(defcustom ebrowse-tree-mode-hook nil
  "*Hook run in each new tree buffer."
  :group 'ebrowse-tree
  :type 'hook)


(defcustom ebrowse-tree-buffer-name "*Tree*"
  "*The default name of class tree buffers."
  :group 'ebrowse-tree
  :type 'string)


(defcustom ebrowse--indentation 4
  "*The amount by which subclasses are indented in the tree."
  :group 'ebrowse-tree
  :type 'integer)


(defcustom ebrowse-source-file-column 40
  "*The column in which source file names are displayed in the tree."
  :group 'ebrowse-tree
  :type 'integer)


(defcustom ebrowse-tree-left-margin 2
  "*Amount of space left at the left side of the tree display.
This space is used to display markers."
  :group 'ebrowse-tree
  :type 'integer)



(defgroup ebrowse-member nil
  "Settings for member buffers."
  :group 'ebrowse)


(defcustom ebrowse-default-declaration-column 25
  "*The column in which member declarations are displayed in member buffers."
  :group 'ebrowse-member
  :type 'integer)


(defcustom ebrowse-default-column-width 25
  "*The width of the columns in member buffers (short display form)."
  :group 'ebrowse-member
  :type 'integer)


(defcustom ebrowse-member-buffer-name "*Members*"
  "*The name of the buffer for member display."
  :group 'ebrowse-member
  :type 'string)


(defcustom ebrowse-member-mode-hook nil
  "*Run in each new member buffer."
  :group 'ebrowse-member
  :type 'hook)



(defgroup ebrowse-faces nil
  "Faces used by Ebrowse."
  :group 'ebrowse)


(defface ebrowse-tree-mark
  '((((min-colors 88)) (:foreground "red1"))
    (t (:foreground "red")))
  "*The face used for the mark character in the tree."
  :group 'ebrowse-faces)
;; backward-compatibility alias
(put 'ebrowse-tree-mark-face 'face-alias 'ebrowse-tree-mark)


(defface ebrowse-root-class
  '((((min-colors 88)) (:weight bold :foreground "blue1"))
    (t (:weight bold :foreground "blue")))
  "*The face used for root classes in the tree."
  :group 'ebrowse-faces)
;; backward-compatibility alias
(put 'ebrowse-root-class-face 'face-alias 'ebrowse-root-class)


(defface ebrowse-file-name
  '((t (:italic t)))
  "*The face for filenames displayed in the tree."
  :group 'ebrowse-faces)
;; backward-compatibility alias
(put 'ebrowse-file-name-face 'face-alias 'ebrowse-file-name)


(defface ebrowse-default
  '((t nil))
  "*Face for everything else in the tree not having other faces."
  :group 'ebrowse-faces)
;; backward-compatibility alias
(put 'ebrowse-default-face 'face-alias 'ebrowse-default)


(defface ebrowse-member-attribute
  '((((min-colors 88)) (:foreground "red1"))
    (t (:foreground "red")))
  "*Face used to display member attributes."
  :group 'ebrowse-faces)
;; backward-compatibility alias
(put 'ebrowse-member-attribute-face 'face-alias 'ebrowse-member-attribute)


(defface ebrowse-member-class
  '((t (:foreground "purple")))
  "*Face used to display the class title in member buffers."
  :group 'ebrowse-faces)
;; backward-compatibility alias
(put 'ebrowse-member-class-face 'face-alias 'ebrowse-member-class)


(defface ebrowse-progress
  '((((min-colors 88)) (:background "blue1"))
    (t (:background "blue")))
  "*Face for progress indicator."
  :group 'ebrowse-faces)
;; backward-compatibility alias
(put 'ebrowse-progress-face 'face-alias 'ebrowse-progress)



;;; Utilities.

(defun ebrowse-some (predicate vector)
  "Return true if PREDICATE is true of some element of VECTOR.
If so, return the value returned by PREDICATE."
  (let ((length (length vector))
	(i 0)
	result)
    (while (and (< i length) (not result))
      (setq result (funcall predicate (aref vector i))
	    i (1+ i)))
    result))


(defun ebrowse-every (predicate vector)
  "Return true if PREDICATE is true of every element of VECTOR."
  (let ((length (length vector))
	(i 0)
	(result t))
    (while (and (< i length) result)
      (setq result (funcall predicate (aref vector i))
	    i (1+ i)))
    result))


(defun ebrowse-position (item list &optional test)
  "Return the position of ITEM in LIST or nil if not found.
Compare items with `eq' or TEST if specified."
  (let ((i 0) found)
    (cond (test
	   (while list
	     (when (funcall test item (car list))
	       (setq found i list nil))
	     (setq list (cdr list) i (1+ i))))
	  (t
	   (while list
	     (when (eq item (car list))
	       (setq found i list nil))
	     (setq list (cdr list) i (1+ i)))))
    found))


(defun ebrowse-delete-if-not (predicate list)
  "Remove elements not satisfying PREDICATE from LIST and return the result.
This is a destructive operation."
  (let (result)
    (while list
      (let ((next (cdr list)))
	(when (funcall predicate (car list))
	  (setq result (nconc result list))
	  (setf (cdr list) nil))
	(setq list next)))
    result))


(defmacro ebrowse-output (&rest body)
  "Eval BODY with a writable current buffer.
Preserve buffer's modified state."
  (let ((modified (make-symbol "--ebrowse-output--")))
    `(let (buffer-read-only (,modified (buffer-modified-p)))
       (unwind-protect
	   (progn ,@body)
	 (set-buffer-modified-p ,modified)))))


(defmacro ebrowse-ignoring-completion-case (&rest body)
  "Eval BODY with `completion-ignore-case' bound to t."
  `(let ((completion-ignore-case t))
     ,@body))


(defmacro ebrowse-save-selective (&rest body)
  "Eval BODY with `selective-display' restored at the end."
  (let ((var (make-symbol "var")))
    `(let ((,var selective-display))
       (unwind-protect
	   (progn ,@body)
	 (setq selective-display ,var)))))


(defmacro ebrowse-for-all-trees (spec &rest body)
  "For all trees in SPEC, eval BODY."
  (let ((var (make-symbol "var"))
	(spec-var (car spec))
	(array (cadr spec)))
    `(loop for ,var being the symbols of ,array
	   as ,spec-var = (get ,var 'ebrowse-root) do
	   (when (vectorp ,spec-var)
	     ,@body))))

;;; Set indentation for macros above.

(put 'ebrowse-output 'lisp-indent-hook 0)
(put 'ebrowse-ignoring-completion-case 'lisp-indent-hook 0)
(put 'ebrowse-save-selective 'lisp-indent-hook 0)
(put 'ebrowse-for-all-trees 'lisp-indent-hook 1)


(defsubst ebrowse-set-face (start end face)
  "Set face of a region START END to FACE."
  (overlay-put (make-overlay start end) 'face face))


(defun ebrowse-completing-read-value (prompt table initial-input)
  "Read a string in the minibuffer, with completion.
Case is ignored in completions.

PROMPT is a string to prompt with; normally it ends in a colon and a space.
TABLE is an alist whose elements' cars are strings, or an obarray.
TABLE can also be a function to do the completion itself.
If INITIAL-INPUT is non-nil, insert it in the minibuffer initially.
If it is (STRING . POSITION), the initial input
is STRING, but point is placed POSITION characters into the string."
  (ebrowse-ignoring-completion-case
    (completing-read prompt table nil t initial-input)))


(defun ebrowse-value-in-buffer (sym buffer)
  "Return the value of SYM in BUFFER."
  (let ((old-buffer (current-buffer)))
    (unwind-protect
        (progn
          (set-buffer buffer)
          (symbol-value sym))
      (set-buffer old-buffer))))


(defun ebrowse-rename-buffer (new-name)
  "Rename current buffer to NEW-NAME.
If a buffer with name NEW-NAME already exists, delete it first."
  (let ((old-buffer (get-buffer new-name)))
    (unless (eq old-buffer (current-buffer))
      (when old-buffer
        (save-excursion (kill-buffer old-buffer)))
      (rename-buffer new-name))))


(defun ebrowse-trim-string (string)
  "Return a copy of STRING with leading white space removed.
Replace sequences of newlines with a single space."
  (when (string-match "^[ \t\n\r]+" string)
    (setq string (substring string (match-end 0))))
  (loop while (string-match "[\n]+" string)
        finally return string do
	(setq string (replace-match " " nil t string))))


(defun ebrowse-width-of-drawable-area ()
  "Return the width of the display area for the current buffer.
If buffer is displayed in a window, use that window's width,
otherwise use the current frame's width."
  (let ((window (get-buffer-window (current-buffer))))
    (if window
        (window-width window)
      (frame-width))))


;;; Structure definitions

(defstruct (ebrowse-hs (:type vector) :named)
  "Header structure found at the head of BROWSE files."
  ;; A version string that is compared against the version number of
  ;; the Lisp package when the file is loaded.  This is done to
  ;; detect file format changes.
  version
  ;; Command line options used for producing the BROWSE file.
  command-line-options
  ;; The following slot is currently not used.  It's kept to keep
  ;; the file format compatible.
  unused
  ;; A slot that is filled out after the tree is loaded.  This slot is
  ;; set to a hash table mapping members to lists of classes in which
  ;; they are defined.
  member-table)


(defstruct (ebrowse-ts (:type vector) :named)
  "Tree structure.
Following the header structure, a BROWSE file contains a number
of `ebrowse-ts' structures, each one describing one root class of
the class hierarchy with all its subclasses."
  ;; A `ebrowse-cs' structure describing the root class.
  class
  ;; A list of `ebrowse-ts' structures for all subclasses.
  subclasses
  ;; Lists of `ebrowse-ms' structures for each member in a group of
  ;; members.
  member-variables member-functions static-variables static-functions
  friends types
  ;; List of `ebrowse-ts' structures for base classes.  This slot is
  ;; filled at load time.
  base-classes
  ;; A marker slot used in the tree buffer (can be saved back to disk.
  mark)


(defstruct (ebrowse-bs (:type vector) :named)
  "Common sub-structure.
A common structure defining an occurrence of some name in the
source files."
  ;; The class or member name as a string constant
  name
  ;; An optional string for the scope of nested classes or for
  ;; namespaces.
  scope
  ;; Various flags describing properties of classes/members, e.g. is
  ;; template, is const etc.
  flags
  ;; File in which the entity is found.  If this is part of a
  ;; `ebrowse-ms' member description structure, and FILE is nil, then
  ;; search for the name in the SOURCE-FILE of the members class.
  file
  ;; Regular expression to search for.  This slot can be a number in
  ;; which case the number is the file position at which the regular
  ;; expression is found in a separate regexp file (see the header
  ;; structure).  This slot can be nil in which case the regular
  ;; expression will be generated from the class/member name.
  pattern
  ;; The buffer position at which the search for the class or member
  ;; will start.
  point)


(defstruct (ebrowse-cs (:include ebrowse-bs) (:type vector) :named)
  "Class structure.
This is the structure stored in the CLASS slot of a `ebrowse-ts'
structure.  It describes the location of the class declaration."
  source-file)


(defstruct (ebrowse-ms (:include ebrowse-bs) (:type vector) :named)
  "Member structure.
This is the structure describing a single member.  The `ebrowse-ts'
structure contains various lists for the different types of
members."
  ;; Public, protected, private
  visibility
  ;; The file in which the member's definition can be found.
  definition-file
  ;; Same as PATTERN above, but for the member definition.
  definition-pattern
  ;; Same as POINT above but for member definition.
  definition-point)



;;; Some macros to access the FLAGS slot of a MEMBER.

(defsubst ebrowse-member-bit-set-p (member bit)
  "Value is non-nil if MEMBER's bit BIT is set."
  (/= 0 (logand (ebrowse-bs-flags member) bit)))


(defsubst ebrowse-virtual-p (member)
  "Value is non-nil if MEMBER is virtual."
  (ebrowse-member-bit-set-p member 1))


(defsubst ebrowse-inline-p (member)
  "Value is non-nil if MEMBER is inline."
  (ebrowse-member-bit-set-p member 2))


(defsubst ebrowse-const-p (member)
  "Value is non-nil if MEMBER is const."
  (ebrowse-member-bit-set-p member 4))


(defsubst ebrowse-pure-virtual-p (member)
  "Value is non-nil if MEMBER is a pure virtual function."
  (ebrowse-member-bit-set-p member 8))


(defsubst ebrowse-mutable-p (member)
  "Value is non-nil if MEMBER is mutable."
  (ebrowse-member-bit-set-p member 16))


(defsubst ebrowse-template-p (member)
  "Value is non-nil if MEMBER is a template."
  (ebrowse-member-bit-set-p member 32))


(defsubst ebrowse-explicit-p (member)
  "Value is non-nil if MEMBER is explicit."
  (ebrowse-member-bit-set-p member 64))


(defsubst ebrowse-throw-list-p (member)
  "Value is non-nil if MEMBER has a throw specification."
  (ebrowse-member-bit-set-p member 128))


(defsubst ebrowse-extern-c-p (member)
  "Value is non-nil if MEMBER.is `extern \"C\"'."
  (ebrowse-member-bit-set-p member 256))


(defsubst ebrowse-define-p (member)
  "Value is non-nil if MEMBER is a define."
  (ebrowse-member-bit-set-p member 512))


(defconst ebrowse-version-string "ebrowse 5.0"
  "Version string expected in BROWSE files.")


(defconst ebrowse-globals-name "*Globals*"
  "The name used for the surrogate class.containing global entities.
This must be the same that `ebrowse' uses.")


(defvar ebrowse--last-regexp nil
  "Last regular expression searched for in tree and member buffers.
Each tree and member buffer maintains its own search history.")
(make-variable-buffer-local 'ebrowse--last-regexp)


(defconst ebrowse-member-list-accessors
  '(ebrowse-ts-member-variables
    ebrowse-ts-member-functions
    ebrowse-ts-static-variables
    ebrowse-ts-static-functions
    ebrowse-ts-friends
    ebrowse-ts-types)
  "List of accessors for member lists.
Each element is the symbol of an accessor function.
The nth element must be the accessor for the nth member list
in an `ebrowse-ts' structure.")


;;; FIXME: Add more doc strings for the buffer-local variables below.

(defvar ebrowse--tree-obarray nil
  "Obarray holding all `ebrowse-ts' structures of a class tree.
Buffer-local in Ebrowse buffers.")


(defvar ebrowse--tags-file-name nil
  "File from which BROWSE file was loaded.
Buffer-local in Ebrowse buffers.")


(defvar ebrowse--header nil
  "Header structure of type `ebrowse-hs' of a class tree.
Buffer-local in Ebrowse buffers.")


(defvar ebrowse--frozen-flag nil
  "Non-nil means an Ebrowse buffer won't be reused.
Buffer-local in Ebrowse buffers.")


(defvar ebrowse--show-file-names-flag nil
  "Non-nil means show file names in a tree buffer.
Buffer-local in Ebrowse tree buffers.")


(defvar ebrowse--long-display-flag nil
  "Non-nil means show members in long display form.
Buffer-local in Ebrowse member buffers.")


(defvar ebrowse--n-columns nil
  "Number of columns to display for short member display form.
Buffer-local in Ebrowse member buffers.")


(defvar ebrowse--column-width nil
  "Width of a columns to display for short member display form.
Buffer-local in Ebrowse member buffers.")


(defvar ebrowse--virtual-display-flag nil
  "Non-nil means display virtual members in a member buffer.
Buffer-local in Ebrowse member buffers.")


(defvar ebrowse--inline-display-flag nil
  "Non-nil means display inline members in a member buffer.
Buffer-local in Ebrowse member buffers.")


(defvar ebrowse--const-display-flag nil
  "Non-nil means display const members in a member buffer.
Buffer-local in Ebrowse member buffers.")


(defvar ebrowse--pure-display-flag nil
  "Non-nil means display pure virtual members in a member buffer.
Buffer-local in Ebrowse member buffers.")


(defvar ebrowse--filters nil
  "Filter for display of public, protected, and private members.
This is a vector of three elements.  An element nil means the
corresponding members are not shown.
Buffer-local in Ebrowse member buffers.")


(defvar ebrowse--show-inherited-flag nil
  "Non-nil means display inherited members in a member buffer.
Buffer-local in Ebrowse member buffers.")


(defvar ebrowse--attributes-flag nil
  "Non-nil means display member attributes in a member buffer.
Buffer-local in Ebrowse member buffers.")


(defvar ebrowse--source-regexp-flag nil
  "Non-nil means display member regexps in a member buffer.
Buffer-local in Ebrowse member buffers.")


(defvar ebrowse--displayed-class nil
  "Class displayed in a member buffer, a `ebrowse-ts' structure.
Buffer-local in Ebrowse member buffers.")


(defvar ebrowse--accessor nil
  "Member list displayed in a member buffer.
This is a symbol whose function definition is an accessor for the
member list in `ebrowse-cs' structures.
Buffer-local in Ebrowse member buffers.")


(defvar ebrowse--member-list nil
  "The list of `ebrowse-ms' structures displayed in a member buffer.
Buffer-local in Ebrowse member buffers.")


(defvar ebrowse--decl-column nil
  "Column in which declarations are displayed in member buffers.
Buffer-local in Ebrowse member buffers.")


(defvar ebrowse--frame-configuration nil
  "Frame configuration saved when viewing a class/member in another frame.
Buffer-local in Ebrowse buffers.")


(defvar ebrowse--view-exit-action nil
  "Action to perform after viewing a class/member.
Either `kill-buffer' or nil.
Buffer-local in Ebrowse buffers.")


(defvar ebrowse--tree nil
  "Class tree.
Buffer-local in Ebrowse buffers.")


;;; Temporaries used to communicate with `ebrowse-find-pattern'.

(defvar ebrowse-temp-position-to-view nil)
(defvar ebrowse-temp-info-to-view nil)


(defvar ebrowse-tree-mode-map ()
  "The keymap used in tree mode buffers.")


(defvar ebrowse--member-mode-strings nil
  "Strings displayed in the mode line of member buffers.")


(defvar ebrowse-member-mode-map ()
  "The keymap used in the member buffers.")


;;; Define mode line titles for each member list.

(put 'ebrowse-ts-member-variables 'ebrowse-title "Member Variables")
(put 'ebrowse-ts-member-functions 'ebrowse-title "Member Functions")
(put 'ebrowse-ts-static-variables 'ebrowse-title "Static Variables")
(put 'ebrowse-ts-static-functions 'ebrowse-title "Static Functions")
(put 'ebrowse-ts-friends 'ebrowse-title "Friends")
(put 'ebrowse-ts-types 'ebrowse-title "Types")

(put 'ebrowse-ts-member-variables 'ebrowse-global-title "Global Variables")
(put 'ebrowse-ts-member-functions 'ebrowse-global-title "Global Functions")
(put 'ebrowse-ts-static-variables 'ebrowse-global-title "Static Variables")
(put 'ebrowse-ts-static-functions 'ebrowse-global-title "Static Functions")
(put 'ebrowse-ts-friends 'ebrowse-global-title "Defines")
(put 'ebrowse-ts-types 'ebrowse-global-title "Types")



;;; Operations on `ebrowse-ts' structures

(defun ebrowse-files-table (&optional marked-only)
  "Return an obarray containing all files mentioned in the current tree.
The tree is expected in the buffer-local variable `ebrowse--tree-obarray'.
MARKED-ONLY non-nil means include marked classes only."
  (let ((files (make-hash-table :test 'equal))
	(i -1))
    (ebrowse-for-all-trees (tree ebrowse--tree-obarray)
      (when (or (not marked-only) (ebrowse-ts-mark tree))
	(let ((class (ebrowse-ts-class tree)))
	  (when (zerop (% (incf i) 20))
	    (ebrowse-show-progress "Preparing file list" (zerop i)))
	  ;; Add files mentioned in class description
	  (let ((source-file (ebrowse-cs-source-file class))
		(file        (ebrowse-cs-file class)))
	    (when source-file
	      (puthash source-file source-file files))
	    (when file
	      (puthash file file files))
	    ;; For all member lists in this class
	    (loop for accessor in ebrowse-member-list-accessors do
		  (loop for m in (funcall accessor tree)
			for file = (ebrowse-ms-file m)
			for def-file = (ebrowse-ms-definition-file m) do
			(when file
			  (puthash file file files))
			(when def-file
			  (puthash def-file def-file files))))))))
    files))


(defun ebrowse-files-list (&optional marked-only)
  "Return a list containing all files mentioned in a tree.
MARKED-ONLY non-nil means include marked classes only."
  (let (list)
    (maphash #'(lambda (file dummy) (setq list (cons file list)))
	     (ebrowse-files-table marked-only))
    list))


(defun* ebrowse-marked-classes-p ()
  "Value is non-nil if any class in the current class tree is marked."
  (ebrowse-for-all-trees (tree ebrowse--tree-obarray)
    (when (ebrowse-ts-mark tree)
      (return-from ebrowse-marked-classes-p tree))))


(defsubst ebrowse-globals-tree-p (tree)
  "Return t if TREE is the one for global entities."
  (string= (ebrowse-bs-name (ebrowse-ts-class tree))
	   ebrowse-globals-name))


(defsubst ebrowse-qualified-class-name (class)
  "Return the name of CLASS with scope prepended, if any."
  (if (ebrowse-cs-scope class)
      (concat (ebrowse-cs-scope class) "::" (ebrowse-cs-name class))
    (ebrowse-cs-name class)))


(defun ebrowse-tree-obarray-as-alist (&optional qualified-names-p)
  "Return an alist describing all classes in a tree.
Each elements in the list has the form (CLASS-NAME . TREE).
CLASS-NAME is the name of the class.  TREE is the
class tree whose root is QUALIFIED-CLASS-NAME.
QUALIFIED-NAMES-P non-nil means return qualified names as CLASS-NAME.
The class tree is found in the buffer-local variable `ebrowse--tree-obarray'."
  (let (alist)
    (if qualified-names-p
	(ebrowse-for-all-trees (tree ebrowse--tree-obarray)
	  (setq alist
		(acons (ebrowse-qualified-class-name (ebrowse-ts-class tree))
		       tree alist)))
      (ebrowse-for-all-trees (tree ebrowse--tree-obarray)
	(setq alist
	      (acons (ebrowse-cs-name (ebrowse-ts-class tree))
		     tree alist))))
    alist))


(defun ebrowse-sort-tree-list (list)
  "Sort a LIST of `ebrowse-ts' structures by qualified class names."
  (sort list
	#'(lambda (a b)
	    (string< (ebrowse-qualified-class-name (ebrowse-ts-class a))
		     (ebrowse-qualified-class-name (ebrowse-ts-class b))))))


(defun ebrowse-class-in-tree (class tree)
  "Search for a class with name CLASS in TREE.
If CLASS is found, return the tail of TREE starting at CLASS.  This function
is used during the load phase where classes appended to a file replace older
class information."
  (let ((tclass (ebrowse-ts-class class))
	found)
    (while (and tree (not found))
      (let ((root-ptr tree))
	(when (string= (ebrowse-qualified-class-name (ebrowse-ts-class (car root-ptr)))
		       (ebrowse-qualified-class-name tclass))
	  (setq found root-ptr))
	(setq tree (cdr tree))))
    found))


(defun ebrowse-base-classes (tree)
  "Return list of base-classes of TREE by searching subclass lists.
This function must be used instead of the struct slot
`base-classes' to access the base-class list directly because it
computes this information lazily."
  (or (ebrowse-ts-base-classes tree)
      (setf (ebrowse-ts-base-classes tree)
	    (loop with to-search = (list tree)
		  with result = nil
		  as search = (pop to-search)
		  while search finally return result
		  do (ebrowse-for-all-trees (ti ebrowse--tree-obarray)
		       (when (memq search (ebrowse-ts-subclasses ti))
			 (unless (memq ti result)
			   (setq result (nconc result (list ti))))
			 (push ti to-search)))))))


(defun ebrowse-direct-base-classes (tree)
  "Return the list of direct super classes of TREE."
  (let (result)
    (dolist (s (ebrowse-base-classes tree))
      (when (memq tree (ebrowse-ts-subclasses s))
	(setq result (cons s result))))
    result))



;;; Operations on MEMBER structures/lists

(defun ebrowse-name/accessor-alist (tree accessor)
  "Return an alist containing all members of TREE in group ACCESSOR.
ACCESSOR is the accessor function for the member list.
Elements of the result have the form (NAME . ACCESSOR), where NAME
is the member name."
  (loop for member in (funcall accessor tree)
	collect (cons (ebrowse-ms-name member) accessor)))


(defun ebrowse-name/accessor-alist-for-visible-members ()
  "Return an alist describing all members visible in the current buffer.
Each element of the list has the form (MEMBER-NAME . ACCESSOR),
where MEMBER-NAME is the member's name, and ACCESSOR is the struct
accessor with which the member's list can be accessed in an `ebrowse-ts'
structure.  The list includes inherited members if these are visible."
  (let* ((list (ebrowse-name/accessor-alist ebrowse--displayed-class
					    ebrowse--accessor)))
    (if ebrowse--show-inherited-flag
	(nconc list
	       (loop for tree in (ebrowse-base-classes
				  ebrowse--displayed-class)
		     nconc (ebrowse-name/accessor-alist
			    tree ebrowse--accessor)))
      list)))


(defun ebrowse-name/accessor-alist-for-class-members ()
  "Like `ebrowse-name/accessor-alist-for-visible-members'.
This function includes members of base classes if base class members
are visible in the buffer."
  (let (list)
    (dolist (func ebrowse-member-list-accessors list)
      (setq list (nconc list (ebrowse-name/accessor-alist
			      ebrowse--displayed-class func)))
      (when ebrowse--show-inherited-flag
	(dolist (class (ebrowse-base-classes ebrowse--displayed-class))
	  (setq list
		(nconc list (ebrowse-name/accessor-alist class func))))))))


;;; Progress indication

(defvar ebrowse-n-boxes 0)
(defconst ebrowse-max-boxes 60)

(defun ebrowse-show-progress (title &optional start)
  "Display a progress indicator.
TITLE is the title of the progress message.  START non-nil means
this is the first progress message displayed."
  (let (message-log-max)
    (when start (setq ebrowse-n-boxes 0))
    (setq ebrowse-n-boxes (mod (1+ ebrowse-n-boxes) ebrowse-max-boxes))
    (message "%s: %s" title
	     (propertize (make-string ebrowse-n-boxes
				      (if (display-color-p) ?\  ?+))
			 'face 'ebrowse-progress))))


;;; Reading a tree from disk

(defun ebrowse-read ()
  "Read `ebrowse-hs' and `ebrowse-ts' structures in the current buffer.
Return a list (HEADER TREE) where HEADER is the file header read
and TREE is a list of `ebrowse-ts' structures forming the class tree."
  (let ((header (condition-case nil
		    (read (current-buffer))
		  (error (error "No Ebrowse file header found"))))
	tree)
    ;; Check file format.
    (unless (ebrowse-hs-p header)
      (error "No Ebrowse file header found"))
    (unless (string= (ebrowse-hs-version header) ebrowse-version-string)
      (error "File has wrong version `%s' (`%s' expected)"
	     (ebrowse-hs-version header) ebrowse-version-string))
    ;; Read Lisp objects.  Temporarily increase `gc-cons-threshold' to
    ;; prevent a GC that would not free any memory.
    (let ((gc-cons-threshold 2000000))
      (while (not (progn (skip-chars-forward " \t\n\r") (eobp)))
	(let* ((root (read (current-buffer)))
	       (old-root-ptr (ebrowse-class-in-tree root tree)))
	  (ebrowse-show-progress "Reading data" (null tree))
	  (if old-root-ptr
	      (setcar old-root-ptr root)
	    (push root tree)))))
    (garbage-collect)
    (list header tree)))


(defun ebrowse-revert-tree-buffer-from-file (ignore-auto-save noconfirm)
  "Function installed as `revert-buffer-function' in tree buffers.
See that variable's documentation for the meaning of IGNORE-AUTO-SAVE and
NOCONFIRM."
  (when (or noconfirm (yes-or-no-p "Revert tree from disk? "))
    (loop for member-buffer in (ebrowse-same-tree-member-buffer-list)
	  do (kill-buffer member-buffer))
    (erase-buffer)
    (with-no-warnings
      (insert-file (or buffer-file-name ebrowse--tags-file-name)))
    (ebrowse-tree-mode)
    (current-buffer)))


(defun ebrowse-create-tree-buffer (tree tags-file header obarray pop)
  "Create a new tree buffer for tree TREE.
The tree was loaded from file TAGS-FILE.
HEADER is the header structure of the file.
OBARRAY is an obarray with a symbol for each class in the tree.
POP non-nil means popup the buffer up at the end.
Return the buffer created."
  (let ((name ebrowse-tree-buffer-name))
    (set-buffer (get-buffer-create name))
    (ebrowse-tree-mode)
    (setq ebrowse--tree tree
	  ebrowse--tags-file-name tags-file
	  ebrowse--tree-obarray obarray
	  ebrowse--header header
	  ebrowse--frozen-flag nil)
    (ebrowse-redraw-tree)
    (set-buffer-modified-p nil)
    (case pop
      (switch (switch-to-buffer name))
      (pop (pop-to-buffer name)))
    (current-buffer)))



;;; Operations for member obarrays

(defun ebrowse-fill-member-table ()
  "Return an obarray holding all members of all classes in the current tree.

For each member, a symbol is added to the obarray.  Members are
extracted from the buffer-local tree `ebrowse--tree-obarray'.

Each symbol has its property `ebrowse-info' set to a list (TREE MEMBER-LIST
MEMBER) where TREE is the tree in which the member is defined,
MEMBER-LIST is a symbol describing the member list in which the member
is found, and MEMBER is a MEMBER structure describing the member.

The slot `member-table' of the buffer-local header structure of
type `ebrowse-hs' is set to the resulting obarray."
  (let ((members (make-hash-table :test 'equal))
	(i -1))
    (setf (ebrowse-hs-member-table ebrowse--header) nil)
    (garbage-collect)
    ;; For all classes...
    (ebrowse-for-all-trees (c ebrowse--tree-obarray)
      (when (zerop (% (incf i) 10))
	(ebrowse-show-progress "Preparing member lookup" (zerop i)))
      (loop for f in ebrowse-member-list-accessors do
	    (loop for m in (funcall f c) do
		  (let* ((member-name (ebrowse-ms-name m))
			 (value (gethash member-name members)))
		    (push (list c f m) value)
		    (puthash member-name value members)))))
    (setf (ebrowse-hs-member-table ebrowse--header) members)))


(defun ebrowse-member-table (header)
  "Return the member obarray.  Build it if it hasn't been set up yet.
HEADER is the tree header structure of the class tree."
  (when (null (ebrowse-hs-member-table header))
    (loop for buffer in (ebrowse-browser-buffer-list)
	  until (eq header (ebrowse-value-in-buffer 'ebrowse--header buffer))
	  finally do
	  (save-excursion
	    (set-buffer buffer)
	    (ebrowse-fill-member-table))))
  (ebrowse-hs-member-table header))



;;; Operations on TREE obarrays

(defun ebrowse-build-tree-obarray (tree)
  "Make sure every class in TREE is represented by a unique object.
Build obarray of all classes in TREE."
  (let ((classes (make-vector 127 0)))
    ;; Add root classes...
    (loop for root in tree
	  as sym =
	  (intern (ebrowse-qualified-class-name (ebrowse-ts-class root)) classes)
	  do (unless (get sym 'ebrowse-root)
	       (setf (get sym 'ebrowse-root) root)))
    ;; Process subclasses
    (ebrowse-insert-supers tree classes)
    classes))


(defun ebrowse-insert-supers (tree classes)
  "Build base class lists in class tree TREE.
CLASSES is an obarray used to collect classes.

Helper function for `ebrowse-build-tree-obarray'.  Base classes should
be ordered so that immediate base classes come first, then the base
class of the immediate base class and so on.  This means that we must
construct the base-class list top down with adding each level at the
beginning of the base-class list.

We have to be cautious here not to end up in an infinite recursion
if for some reason a circle is in the inheritance graph."
  (loop for class in tree
	as subclasses = (ebrowse-ts-subclasses class) do
	;; Make sure every class is represented by a unique object
	(loop for subclass on subclasses
	      as sym = (intern
			(ebrowse-qualified-class-name (ebrowse-ts-class (car subclass)))
			classes)
	      as next = nil
	      do
	      ;; Replace the subclass tree with the one found in
	      ;; CLASSES if there is already an entry for that class
	      ;; in it. Otherwise make a new entry.
	      ;;
	      ;; CAVEAT: If by some means (e.g., use of the
	      ;; preprocessor in class declarations, a name is marked
	      ;; as a subclass of itself on some path, we would end up
	      ;; in an endless loop. We have to omit subclasses from
	      ;; the recursion that already have been processed.
	      (if (get sym 'ebrowse-root)
		  (setf (car subclass) (get sym 'ebrowse-root))
		(setf (get sym 'ebrowse-root) (car subclass))))
	;; Process subclasses
	(ebrowse-insert-supers subclasses classes)))


;;; Tree buffers

(unless ebrowse-tree-mode-map
  (let ((map (make-keymap)))
    (setf ebrowse-tree-mode-map map)
    (suppress-keymap map)

    (when (display-mouse-p)
      (define-key map [down-mouse-3] 'ebrowse-mouse-3-in-tree-buffer)
      (define-key map [mouse-2] 'ebrowse-mouse-2-in-tree-buffer)
      (define-key map [down-mouse-1] 'ebrowse-mouse-1-in-tree-buffer))

    (let ((map1 (make-sparse-keymap)))
      (suppress-keymap map1 t)
      (define-key map "L" map1)
      (define-key map1 "d" 'ebrowse-tree-command:show-friends)
      (define-key map1 "f" 'ebrowse-tree-command:show-member-functions)
      (define-key map1 "F" 'ebrowse-tree-command:show-static-member-functions)
      (define-key map1 "t" 'ebrowse-tree-command:show-types)
      (define-key map1 "v" 'ebrowse-tree-command:show-member-variables)
      (define-key map1 "V" 'ebrowse-tree-command:show-static-member-variables))

    (let ((map1 (make-sparse-keymap)))
      (suppress-keymap map1 t)
      (define-key map "M" map1)
      (define-key map1 "a" 'ebrowse-mark-all-classes)
      (define-key map1 "t" 'ebrowse-toggle-mark-at-point))

    (let ((map1 (make-sparse-keymap)))
      (suppress-keymap map1 t)
      (define-key map "T" map1)
      (define-key map1 "f" 'ebrowse-toggle-file-name-display)
      (define-key map1 "s" 'ebrowse-show-file-name-at-point)
      (define-key map1 "w" 'ebrowse-set-tree-indentation)
      (define-key map "x" 'ebrowse-statistics))

    (define-key map "n" 'ebrowse-repeat-member-search)
    (define-key map "q" 'bury-buffer)
    (define-key map "*" 'ebrowse-expand-all)
    (define-key map "+" 'ebrowse-expand-branch)
    (define-key map "-" 'ebrowse-collapse-branch)
    (define-key map "/" 'ebrowse-read-class-name-and-go)
    (define-key map " " 'ebrowse-view-class-declaration)
    (define-key map "?" 'describe-mode)
    (define-key map "\C-i" 'ebrowse-pop/switch-to-member-buffer-for-same-tree)
    (define-key map "\C-k" 'ebrowse-remove-class-at-point)
    (define-key map "\C-l" 'ebrowse-redraw-tree)
    (define-key map "\C-m" 'ebrowse-find-class-declaration)))



;;; Tree-mode - mode for tree buffers

;;;###autoload
(defun ebrowse-tree-mode ()
  "Major mode for Ebrowse class tree buffers.
Each line corresponds to a class in a class tree.
Letters do not insert themselves, they are commands.
File operations in the tree buffer work on class tree data structures.
E.g.\\[save-buffer] writes the tree to the file it was loaded from.

Tree mode key bindings:
\\{ebrowse-tree-mode-map}"
  (interactive)
  (let* ((ident (propertized-buffer-identification "C++ Tree"))
	 header tree buffer-read-only)

    (kill-all-local-variables)
    (use-local-map ebrowse-tree-mode-map)
    (buffer-disable-undo)

    (unless (zerop (buffer-size))
      (goto-char (point-min))
      (multiple-value-setq (header tree) (ebrowse-read))
      (message "Sorting. Please be patient...")
      (setq tree (ebrowse-sort-tree-list tree))
      (erase-buffer)
      (message nil))

    (mapc 'make-local-variable
	  '(ebrowse--tags-file-name
	    ebrowse--indentation
	    ebrowse--tree
	    ebrowse--header
	    ebrowse--show-file-names-flag
	    ebrowse--frozen-flag
	    ebrowse--tree-obarray
	    revert-buffer-function))

    (setf ebrowse--show-file-names-flag nil
	  ebrowse--tree-obarray (make-vector 127 0)
	  ebrowse--frozen-flag nil
	  major-mode 'ebrowse-tree-mode
	  mode-name "Ebrowse-Tree"
	  mode-line-buffer-identification ident
	  buffer-read-only t
	  selective-display t
	  selective-display-ellipses t
	  revert-buffer-function 'ebrowse-revert-tree-buffer-from-file
	  ebrowse--header header
	  ebrowse--tree tree
	  ebrowse--tags-file-name (buffer-file-name)
	  ebrowse--tree-obarray (and tree (ebrowse-build-tree-obarray tree))
	  ebrowse--frozen-flag nil)

    (add-hook 'local-write-file-hooks 'ebrowse-write-file-hook-fn)
    (modify-syntax-entry ?_ (char-to-string (char-syntax ?a)))
    (when tree
      (ebrowse-redraw-tree)
      (set-buffer-modified-p nil))
    (run-mode-hooks 'ebrowse-tree-mode-hook)))



(defun ebrowse-update-tree-buffer-mode-line ()
  "Update the tree buffer mode line."
  (ebrowse-rename-buffer (if ebrowse--frozen-flag
			     (ebrowse-frozen-tree-buffer-name
			      ebrowse--tags-file-name)
			   ebrowse-tree-buffer-name))
  (force-mode-line-update))



;;; Removing classes from trees

(defun ebrowse-remove-class-and-kill-member-buffers (tree class)
  "Remove from TREE class CLASS.
Kill all member buffers still containing a reference to the class."
  (let ((sym (intern-soft (ebrowse-cs-name (ebrowse-ts-class class))
			  ebrowse--tree-obarray)))
    (setf tree (delq class tree)
	  (get sym 'ebrowse-root) nil)
    (dolist (root tree)
      (setf (ebrowse-ts-subclasses root)
	    (delq class (ebrowse-ts-subclasses root))
	    (ebrowse-ts-base-classes root) nil)
      (ebrowse-remove-class-and-kill-member-buffers
       (ebrowse-ts-subclasses root) class))
    (ebrowse-kill-member-buffers-displaying class)
    tree))


(defun ebrowse-remove-class-at-point (forced)
  "Remove the class point is on from the class tree.
Do not ask for confirmation if FORCED is non-nil."
  (interactive "P")
  (let* ((class (ebrowse-tree-at-point))
	 (class-name (ebrowse-cs-name (ebrowse-ts-class class)))
	 (subclasses (ebrowse-ts-subclasses class)))
    (cond ((or forced
	       (y-or-n-p (concat "Delete class " class-name "? ")))
	   (setf ebrowse--tree (ebrowse-remove-class-and-kill-member-buffers
				ebrowse--tree class))
	   (set-buffer-modified-p t)
	   (message "%s %sdeleted." class-name
		    (if subclasses "and derived classes " ""))
	   (ebrowse-redraw-tree))
	  (t (message "Aborted")))))



;;; Marking classes in the tree buffer

(defun ebrowse-toggle-mark-at-point (&optional n-times)
  "Toggle mark for class cursor is on.
If given a numeric N-TIMES argument, mark that many classes."
  (interactive "p")
  (let (to-change pnt)
    ;; Get the classes whose mark must be toggled. Note that
    ;; ebrowse-tree-at-point might issue an error.
    (condition-case error
	(loop repeat (or n-times 1)
	      as tree = (ebrowse-tree-at-point)
	      do (progn
		   (setf (ebrowse-ts-mark tree) (not (ebrowse-ts-mark tree)))
		   (forward-line 1)
		   (push tree to-change)))
      (error nil))
    (save-excursion
      ;; For all these classes, reverse the mark char in the display
      ;; by a regexp replace over the whole buffer. The reason for this
      ;; is that classes might have multiple base classes. If this is
      ;; the case, they are displayed more than once in the tree.
      (ebrowse-output
	(loop for tree in to-change
	      as regexp = (concat "^.*\\b"
				  (regexp-quote
				   (ebrowse-cs-name (ebrowse-ts-class tree)))
				  "\\b")
	      do
	      (goto-char (point-min))
	      (loop while (re-search-forward regexp nil t)
		    do (progn
			 (goto-char (match-beginning 0))
			 (delete-char 1)
			 (insert-char (if (ebrowse-ts-mark tree) ?> ? ) 1)
			 (ebrowse-set-mark-props (1- (point)) (point) tree)
			 (goto-char (match-end 0)))))))))


(defun ebrowse-mark-all-classes (prefix)
  "Unmark, with PREFIX mark, all classes in the tree."
  (interactive "P")
  (ebrowse-for-all-trees (tree ebrowse--tree-obarray)
    (setf (ebrowse-ts-mark tree) prefix))
  (ebrowse-redraw-marks (point-min) (point-max)))


(defun ebrowse-redraw-marks (start end)
  "Display class marker signs in the tree between START and END."
  (interactive)
  (save-excursion
    (ebrowse-output
      (catch 'end
	(goto-char (point-min))
	(dolist (root ebrowse--tree)
	  (ebrowse-draw-marks-fn root start end))))
    (ebrowse-update-tree-buffer-mode-line)))


(defun ebrowse-draw-marks-fn (tree start end)
  "Display class marker signs in TREE between START and END."
  (when (>= (point) start)
    (delete-char 1)
    (insert (if (ebrowse-ts-mark tree) ?> ? ))
    (ebrowse-set-mark-props (1- (point)) (point) tree))
  (forward-line 1)
  (when (> (point) end)
    (throw 'end nil))
  (dolist (sub (ebrowse-ts-subclasses tree))
    (ebrowse-draw-marks-fn sub start end)))



;;; File name display in tree buffers

(defun ebrowse-show-file-name-at-point (prefix)
  "Show filename in the line point is in.
With PREFIX, insert that many filenames."
  (interactive "p")
  (unless ebrowse--show-file-names-flag
    (ebrowse-output
      (dotimes (i prefix)
	(let ((tree (ebrowse-tree-at-point))
	      start
	      file-name-existing)
	  (beginning-of-line)
	  (skip-chars-forward " \t*a-zA-Z0-9_")
	  (setq start (point)
		file-name-existing (looking-at "("))
	  (delete-region start (save-excursion (end-of-line) (point)))
	  (unless file-name-existing
	    (indent-to ebrowse-source-file-column)
	    (insert "(" (or (ebrowse-cs-file
			     (ebrowse-ts-class tree))
			    "unknown")
		    ")"))
	  (ebrowse-set-face start (point) 'ebrowse-file-name)
	  (beginning-of-line)
	  (forward-line 1))))))


(defun ebrowse-toggle-file-name-display ()
  "Toggle display of filenames in tree buffer."
  (interactive)
  (setf ebrowse--show-file-names-flag (not ebrowse--show-file-names-flag))
  (let ((old-line (count-lines (point-min) (point))))
    (ebrowse-redraw-tree)
    (goto-line old-line)))



;;; General member and tree buffer functions

(defun ebrowse-member-buffer-p (buffer)
  "Value is non-nil if BUFFER is a member buffer."
  (eq (cdr (assoc 'major-mode (buffer-local-variables buffer)))
      'ebrowse-member-mode))


(defun ebrowse-tree-buffer-p (buffer)
  "Value is non-nil if BUFFER is a class tree buffer."
  (eq (cdr (assoc 'major-mode (buffer-local-variables buffer)))
      'ebrowse-tree-mode))


(defun ebrowse-buffer-p (buffer)
  "Value is non-nil if BUFFER is a tree or member buffer."
  (memq (cdr (assoc 'major-mode (buffer-local-variables buffer)))
	'(ebrowse-tree-mode ebrowse-member-mode)))


(defun ebrowse-browser-buffer-list ()
  "Return a list of all tree or member buffers."
  (ebrowse-delete-if-not 'ebrowse-buffer-p (buffer-list)))


(defun ebrowse-member-buffer-list ()
  "Return a list of all member buffers."
  (ebrowse-delete-if-not 'ebrowse-member-buffer-p (buffer-list)))


(defun ebrowse-tree-buffer-list ()
  "Return a list of all tree buffers."
  (ebrowse-delete-if-not 'ebrowse-tree-buffer-p (buffer-list)))


(defun ebrowse-known-class-trees-buffer-list ()
  "Return a list of buffers containing class trees.
The list will contain, for each class tree loaded,
one buffer.  Prefer tree buffers over member buffers."
  (let ((buffers (nconc (ebrowse-tree-buffer-list)
			(ebrowse-member-buffer-list)))
	(set (make-hash-table))
	result)
    (dolist (buffer buffers)
      (let ((tree (ebrowse-value-in-buffer 'ebrowse--tree buffer)))
	(unless (gethash tree set)
	  (push buffer result))
	(puthash tree t set)))
    result))


(defun ebrowse-same-tree-member-buffer-list ()
  "Return a list of members buffers with same tree as current buffer."
  (ebrowse-delete-if-not
   #'(lambda (buffer)
       (eq (ebrowse-value-in-buffer 'ebrowse--tree buffer)
	   ebrowse--tree))
   (ebrowse-member-buffer-list)))



(defun ebrowse-pop/switch-to-member-buffer-for-same-tree (arg)
  "Pop to the buffer displaying members.
Switch to buffer if prefix ARG.
If no member buffer exists, make one."
  (interactive "P")
  (let ((buf (or (first (ebrowse-same-tree-member-buffer-list))
		 (get-buffer ebrowse-member-buffer-name)
		 (ebrowse-tree-command:show-member-functions))))
    (when buf
      (if arg
	  (switch-to-buffer buf)
	(pop-to-buffer buf)))
    buf))


(defun ebrowse-switch-to-next-member-buffer ()
  "Switch to next member buffer."
  (interactive)
  (let* ((list (ebrowse-member-buffer-list))
	 (next-list (cdr (memq (current-buffer) list)))
	 (next-buffer (if next-list (car next-list) (car list))))
    (if (eq next-buffer (current-buffer))
	(error "No next buffer")
      (bury-buffer)
      (switch-to-buffer next-buffer))))


(defun ebrowse-kill-member-buffers-displaying (tree)
  "Kill all member buffers displaying TREE."
  (loop for buffer in (ebrowse-member-buffer-list)
	as class = (ebrowse-value-in-buffer 'ebrowse--displayed-class buffer)
	when (eq class tree) do (kill-buffer buffer)))


(defun ebrowse-frozen-tree-buffer-name (tags-file-name)
  "Return the buffer name of a tree which is associated TAGS-FILE-NAME."
  (concat ebrowse-tree-buffer-name " (" tags-file-name ")"))


(defun ebrowse-pop-to-browser-buffer (arg)
  "Pop to a browser buffer from any other buffer.
Pop to member buffer if no prefix ARG, to tree buffer otherwise."
  (interactive "P")
  (let ((buffer (get-buffer (if arg
				ebrowse-tree-buffer-name
			      ebrowse-member-buffer-name))))
    (unless buffer
      (setq buffer
	    (get-buffer (if arg
			    ebrowse-member-buffer-name
			  ebrowse-tree-buffer-name))))
    (unless buffer
      (error "No browser buffer found"))
    (pop-to-buffer buffer)))



;;; Misc tree buffer commands

(defun ebrowse-set-tree-indentation ()
  "Set the indentation width of the tree display."
  (interactive)
  (let ((width (string-to-number (read-from-minibuffer
                                  (concat "Indentation ("
                                          (int-to-string ebrowse--indentation)
                                          "): ")))))
    (when (plusp width)
      (setf ebrowse--indentation width)
      (ebrowse-redraw-tree))))


(defun ebrowse-read-class-name-and-go (&optional class)
  "Position cursor on CLASS.
Read a class name from the minibuffer if CLASS is nil."
  (interactive)
  (ebrowse-ignoring-completion-case
    ;; If no class specified, read the class name from mini-buffer
    (unless class
      (setf class
	    (completing-read "Goto class: "
			     (ebrowse-tree-obarray-as-alist) nil t)))
    (ebrowse-save-selective
      (goto-char (point-min))
      (widen)
      (setf selective-display nil)
      (setq ebrowse--last-regexp (concat "\\b" class "\\b"))
      (if (re-search-forward ebrowse--last-regexp nil t)
	  (progn
	    (goto-char (match-beginning 0))
	    (ebrowse-unhide-base-classes))
	(error "Not found")))))



;;; Showing various kinds of member buffers

(defun ebrowse-tree-command:show-member-variables (arg)
  "Display member variables; with prefix ARG in frozen member buffer."
  (interactive "P")
  (ebrowse-display-member-buffer 'ebrowse-ts-member-variables arg))


(defun ebrowse-tree-command:show-member-functions (&optional arg)
  "Display member functions; with prefix ARG in frozen member buffer."
  (interactive "P")
  (ebrowse-display-member-buffer 'ebrowse-ts-member-functions arg))


(defun ebrowse-tree-command:show-static-member-variables (arg)
  "Display static member variables; with prefix ARG in frozen member buffer."
  (interactive "P")
  (ebrowse-display-member-buffer 'ebrowse-ts-static-variables arg))


(defun ebrowse-tree-command:show-static-member-functions (arg)
  "Display static member functions; with prefix ARG in frozen member buffer."
  (interactive "P")
  (ebrowse-display-member-buffer 'ebrowse-ts-static-functions arg))


(defun ebrowse-tree-command:show-friends (arg)
  "Display friend functions; with prefix ARG in frozen member buffer."
  (interactive "P")
  (ebrowse-display-member-buffer 'ebrowse-ts-friends arg))


(defun ebrowse-tree-command:show-types (arg)
  "Display types defined in a class; with prefix ARG in frozen member buffer."
  (interactive "P")
  (ebrowse-display-member-buffer 'ebrowse-ts-types arg))



;;; Viewing or finding a class declaration

(defun ebrowse-tree-at-point ()
  "Return the class structure for the class point is on."
  (or (get-text-property (point) 'ebrowse-tree)
      (error "Not on a class")))


(defun* ebrowse-view/find-class-declaration (&key view where)
  "View or find the declarator of the class point is on.
VIEW non-nil means view it.  WHERE is additional position info."
  (let* ((class (ebrowse-ts-class (ebrowse-tree-at-point)))
	 (file (ebrowse-cs-file class))
	 (browse-struct (make-ebrowse-bs
			 :name (ebrowse-cs-name class)
			 :pattern (ebrowse-cs-pattern class)
			 :flags (ebrowse-cs-flags class)
			 :file (ebrowse-cs-file class)
			 :point (ebrowse-cs-point class))))
    (ebrowse-view/find-file-and-search-pattern
     browse-struct
     (list ebrowse--header class nil)
     file
     ebrowse--tags-file-name
     view
     where)))


(defun ebrowse-find-class-declaration (prefix-arg)
  "Find a class declaration and position cursor on it.
PREFIX-ARG 4 means find it in another window.
PREFIX-ARG 5 means find it in another frame."
  (interactive "p")
  (ebrowse-view/find-class-declaration
   :view nil
   :where (cond ((= prefix-arg 4) 'other-window)
		((= prefix-arg 5) 'other-frame)
		(t                'this-window))))


(defun ebrowse-view-class-declaration (prefix-arg)
  "View class declaration and position cursor on it.
PREFIX-ARG 4 means view it in another window.
PREFIX-ARG 5 means view it in another frame."
  (interactive "p")
  (ebrowse-view/find-class-declaration
   :view 'view
   :where (cond ((= prefix-arg 4) 'other-window)
		((= prefix-arg 5) 'other-frame)
		(t                'this-window))))



;;; The FIND engine

(defun ebrowse-find-source-file (file tags-file-name)
  "Find source file FILE.
Source files are searched for (a) relative to TAGS-FILE-NAME
which is the path of the BROWSE file from which the class tree was loaded,
and (b) in the directories named in `ebrowse-search-path'."
  (let (file-name
	(try-file (expand-file-name file
				    (file-name-directory tags-file-name))))
    (if (file-readable-p try-file)
	(setq file-name try-file)
      (let ((search-in ebrowse-search-path))
	(while (and search-in
		    (null file-name))
	  (let ((try-file (expand-file-name file (car search-in))))
	    (if (file-readable-p try-file)
		(setq file-name try-file))
	    (setq search-in (cdr search-in))))))
    (unless file-name
      (error "File `%s' not found" file))
    file-name))


(defun ebrowse-view-exit-fn (buffer)
  "Function called when exiting View mode in BUFFER.
Restore frame configuration active before viewing the file,
and possibly kill the viewed buffer."
  (let (exit-action original-frame-configuration)
    (save-excursion
      (set-buffer buffer)
      (setq original-frame-configuration ebrowse--frame-configuration
	    exit-action ebrowse--view-exit-action))
    ;; Delete the frame in which we viewed.
    (mapc 'delete-frame
	  (loop for frame in (frame-list)
	     when (not (assq frame original-frame-configuration))
	     collect frame))
    (when exit-action
      (funcall exit-action buffer))))


(defun ebrowse-view-file-other-frame (file)
  "View a file FILE in another frame.
The new frame is deleted when you quit viewing the file in that frame."
  (interactive)
  (let ((old-frame-configuration (current-frame-configuration))
	(had-a-buf (get-file-buffer file))
	(buf-to-view (find-file-noselect file)))
    (switch-to-buffer-other-frame buf-to-view)
    (make-local-variable 'ebrowse--frame-configuration)
    (setq ebrowse--frame-configuration old-frame-configuration)
    (make-local-variable 'ebrowse--view-exit-action)
    (setq ebrowse--view-exit-action
	  (and (not had-a-buf)
	       (not (buffer-modified-p buf-to-view))
	       'kill-buffer))
    (view-mode-enter (cons (selected-window) (cons (selected-window) t))
		     'ebrowse-view-exit-fn)))

(defun ebrowse-view/find-file-and-search-pattern
  (struc info file tags-file-name &optional view where)
  "Find or view a member or class.
STRUC is an `ebrowse-bs' structure (or a structure including that)
describing what to search.
INFO is a list (HEADER MEMBER-OR-CLASS ACCESSOR).  HEADER is the
header structure of a class tree.  MEMBER-OR-CLASS is either an
`ebrowse-ms' or `ebrowse-cs' structure depending on what is searched.
ACCESSOR is an accessor function for the member list of a member
if MEMBER-OR-CLASS is an `ebrowse-ms'.
FILE is the file to search the member in.
FILE is not taken out of STRUC here because the filename in STRUC
may be nil in which case the filename of the class description is used.
TAGS-FILE-NAME is the name of the BROWSE file from which the
tree was loaded.
If VIEW is non-nil, view file else find the file.
WHERE is either `other-window', `other-frame' or `this-window' and
specifies where to find/view the result."
  (unless file
    (error "Sorry, no file information available for %s"
	   (ebrowse-bs-name struc)))
  ;; Get the source file to view or find.
  (setf file (ebrowse-find-source-file file tags-file-name))
  ;; If current window is dedicated, use another frame.
  (when (window-dedicated-p (selected-window))
    (setf where 'other-window))
  (cond (view
	 (setf ebrowse-temp-position-to-view struc
	       ebrowse-temp-info-to-view info)
	 (unless (boundp 'view-mode-hook)
	   (setq view-mode-hook nil))
	 (push 'ebrowse-find-pattern view-mode-hook)
	 (case where
	   (other-window (view-file-other-window file))
	   (other-frame  (ebrowse-view-file-other-frame file))
	   (t            (view-file file))))
	(t
	 (case where
	   (other-window (find-file-other-window file))
	   (other-frame  (find-file-other-frame file))
	   (t            (find-file file)))
	 (ebrowse-find-pattern struc info))))


(defun ebrowse-symbol-regexp (name)
  "Generate a suitable regular expression for a member or class NAME.
This is `regexp-quote' for most symbols, except for operator names
which may contain whitespace.  For these symbols, replace white
space in the symbol name (generated by BROWSE) with a regular
expression matching any number of whitespace characters."
  (loop with regexp = (regexp-quote name)
	with start = 0
	finally return regexp
	while (string-match "[ \t]+" regexp start)
	do (setq regexp (concat (substring regexp 0 (match-beginning 0))
				"[ \t]*"
				(substring regexp (match-end 0)))
		 start (+ (match-beginning 0) 5))))


(defun ebrowse-class-declaration-regexp (name)
  "Construct a regexp for a declaration of class NAME."
  (concat "^[ \t]*\\(template[ \t\n]*<.*>\\)?"
	  "[ \t\n]*\\(class\\|struct\\|union\\).*\\S_"
	  (ebrowse-symbol-regexp name)
	  "\\S_"))


(defun ebrowse-variable-declaration-regexp (name)
  "Construct a regexp for matching a variable NAME."
  (concat "\\S_" (ebrowse-symbol-regexp name) "\\S_"))


(defun ebrowse-function-declaration/definition-regexp (name)
  "Construct a regexp for matching a function NAME."
  (concat "^[a-zA-Z0-9_:*&<>, \t]*\\S_"
	  (ebrowse-symbol-regexp name)
	  "[ \t\n]*("))


(defun ebrowse-pp-define-regexp (name)
  "Construct a regexp matching a define of NAME."
  (concat "^[ \t]*#[ \t]*define[ \t]+" (regexp-quote name)))


(defun* ebrowse-find-pattern (&optional position info &aux viewing)
  "Find a pattern.

This is a kluge: Ebrowse allows you to find or view a file containing
a pattern.  To be able to do a search in a viewed buffer,
`view-mode-hook' is temporarily set to this function;
`ebrowse-temp-position-to-view' holds what to search for.

INFO is a list (TREE-HEADER TREE-OR-MEMBER MEMBER-LIST)."
  (unless position
    (pop view-mode-hook)
    (setf viewing t
	  position ebrowse-temp-position-to-view
	  info ebrowse-temp-info-to-view))
  (widen)
  (let* ((pattern (ebrowse-bs-pattern position))
	 (start (ebrowse-bs-point position))
	 (offset 100)
	 found)
    (destructuring-bind (header class-or-member member-list) info
      ;; If no pattern is specified, construct one from the member name.
      (when (stringp pattern)
	(setq pattern (concat "^.*" (regexp-quote pattern))))
      ;; Construct a regular expression if none given.
      (unless pattern
	(typecase class-or-member
	  (ebrowse-ms
	   (case member-list
	     ((ebrowse-ts-member-variables
	       ebrowse-ts-static-variables
	       ebrowse-ts-types)
	      (setf pattern (ebrowse-variable-declaration-regexp
			     (ebrowse-bs-name position))))
	     (otherwise
	      (if (ebrowse-define-p class-or-member)
		  (setf pattern (ebrowse-pp-define-regexp (ebrowse-bs-name position)))
		(setf pattern (ebrowse-function-declaration/definition-regexp
			       (ebrowse-bs-name position)))))))
	  (ebrowse-cs
	   (setf pattern (ebrowse-class-declaration-regexp
			  (ebrowse-bs-name position))))))
      ;; Begin searching some OFFSET from the original point where the
      ;; regular expression was found by the parse, and step forward.
      ;; When there is no regular expression in the database and a
      ;; member definition/declaration was not seen by the parser,
      ;; START will be 0.
      (when (and (boundp 'ebrowse-debug)
		 (symbol-value 'ebrowse-debug))
	(y-or-n-p (format "start = %d? " start))
	(y-or-n-p pattern))
      (setf found
	    (loop do (goto-char (max (point-min) (- start offset)))
		  when (re-search-forward pattern (+ start offset) t) return t
		  never (bobp)
		  do (incf offset offset)))
      (cond (found
	     (beginning-of-line)
	     (run-hooks 'ebrowse-view/find-hook))
	    ((numberp (ebrowse-bs-pattern position))
	     (goto-char start)
	     (if ebrowse-not-found-hook
		 (run-hooks 'ebrowse-not-found-hook)
	       (message "Not found")
	       (sit-for 2)))
	    (t
	     (if ebrowse-not-found-hook
		 (run-hooks 'ebrowse-not-found-hook)
	       (unless viewing
		 (error "Not found"))
	       (message "Not found")
	       (sit-for 2)))))))


;;; Drawing the tree

(defun ebrowse-redraw-tree (&optional quietly)
  "Redisplay the complete tree.
QUIETLY non-nil means don't display progress messages."
  (interactive)
  (or quietly (message "Displaying..."))
  (save-excursion
    (ebrowse-output
      (erase-buffer)
      (ebrowse-draw-tree-fn)))
  (ebrowse-update-tree-buffer-mode-line)
  (or quietly (message nil)))


(defun ebrowse-set-mark-props (start end tree)
  "Set text properties for class marker signs between START and END.
TREE denotes the class shown."
  (add-text-properties
   start end
   `(mouse-face highlight ebrowse-what mark ebrowse-tree ,tree
		help-echo "double-mouse-1: mark/unmark"))
  (ebrowse-set-face start end 'ebrowse-tree-mark))


(defun* ebrowse-draw-tree-fn (&aux stack1 stack2 start)
  "Display a single class and recursively its subclasses.
This function may look weird, but this is faster than recursion."
  (setq stack1 (make-list (length ebrowse--tree) 0)
	stack2 (copy-sequence ebrowse--tree))
  (loop while stack2
	as level = (pop stack1)
	as tree = (pop stack2)
	as class = (ebrowse-ts-class tree) do
	(let ((start-of-line (point))
	      start-of-class-name end-of-class-name)
	  ;; Insert mark
	  (insert (if (ebrowse-ts-mark tree) ">" " "))

	  ;; Indent and insert class name
	  (indent-to (+ (* level ebrowse--indentation)
			ebrowse-tree-left-margin))
	  (setq start (point))
	  (insert (ebrowse-qualified-class-name class))

	  ;; If template class, add <>
	  (when (ebrowse-template-p class)
	    (insert "<>"))
	  (ebrowse-set-face start (point) (if (zerop level)
					      'ebrowse-root-class
					    'ebrowse-default))
	  (setf start-of-class-name start
		end-of-class-name (point))
	  ;; If filenames are to be displayed...
	  (when ebrowse--show-file-names-flag
	    (indent-to ebrowse-source-file-column)
	    (setq start (point))
	    (insert "("
		    (or (ebrowse-cs-file class)
			"unknown")
		    ")")
	    (ebrowse-set-face start (point) 'ebrowse-file-name))
	  (ebrowse-set-mark-props start-of-line (1+ start-of-line) tree)
	  (add-text-properties
	   start-of-class-name end-of-class-name
	   `(mouse-face highlight ebrowse-what class-name
			ebrowse-tree ,tree
			help-echo "double-mouse-1: (un)expand tree; mouse-2: member functions, mouse-3: menu"))
	  (insert "\n"))
	;; Push subclasses, if any.
	(when (ebrowse-ts-subclasses tree)
	  (setq stack2
		(nconc (copy-sequence (ebrowse-ts-subclasses tree)) stack2)
		stack1
		(nconc (make-list (length (ebrowse-ts-subclasses tree))
				  (1+ level)) stack1)))))



;;; Expanding/ collapsing tree branches

(defun ebrowse-expand-branch (arg)
  "Expand a sub-tree that has been previously collapsed.
With prefix ARG, expand all sub-trees."
  (interactive "P")
  (if arg
      (ebrowse-expand-all arg)
    (ebrowse-collapse-fn nil)))


(defun ebrowse-collapse-branch (arg)
  "Fold (do no longer display) the subclasses of the current class.
\(The class cursor is on.)  With prefix ARG, fold all trees in the buffer."
  (interactive "P")
  (if arg
      (ebrowse-expand-all (not arg))
    (ebrowse-collapse-fn t)))


(defun ebrowse-expand-all (collapse)
  "Expand or fold all trees in the buffer.
COLLAPSE non-nil means fold them."
  (interactive "P")
  (let ((line-end  (if collapse "^\n" "^\r"))
	(insertion (if collapse "\r"  "\n")))
    (ebrowse-output
      (save-excursion
	(goto-char (point-min))
	(while (not (progn (skip-chars-forward line-end) (eobp)))
	  (when (or (not collapse)
		    (looking-at "\n "))
	    (delete-char 1)
	    (insert insertion))
	  (when collapse
	    (skip-chars-forward "\n ")))))))


(defun ebrowse-unhide-base-classes ()
  "Unhide the line the cursor is on and all base classes."
  (ebrowse-output
    (save-excursion
      (let (indent last-indent)
	(skip-chars-backward "^\r\n")
	(when (not (looking-at "[\r\n][^ \t]"))
	  (skip-chars-forward "\r\n \t")
	  (while (and (or (null last-indent) ;first time
			  (> indent 1))	;not root class
		      (re-search-backward "[\r\n][ \t]*" nil t))
	    (setf indent (- (match-end 0)
			    (match-beginning 0)))
	    (when (or (null last-indent)
		      (< indent last-indent))
	      (setf last-indent indent)
	      (when (looking-at "\r")
		(delete-char 1)
		(insert 10)))
	    (backward-char 1)))))))


(defun ebrowse-hide-line (collapse)
  "Hide/show a single line in the tree.
COLLAPSE non-nil means hide."
  (save-excursion
    (ebrowse-output
      (skip-chars-forward "^\r\n")
      (delete-char 1)
      (insert (if collapse 13 10)))))


(defun ebrowse-collapse-fn (collapse)
  "Collapse or expand a branch of the tree.
COLLAPSE non-nil means collapse the branch."
  (ebrowse-output
    (save-excursion
      (beginning-of-line)
      (skip-chars-forward "> \t")
      (let ((indentation (current-column)))
	(while (and (not (eobp))
		    (save-excursion
		      (skip-chars-forward "^\r\n")
		      (goto-char (1+ (point)))
		      (skip-chars-forward "> \t")
		      (> (current-column) indentation)))
	  (ebrowse-hide-line collapse)
	  (skip-chars-forward "^\r\n")
	  (goto-char (1+ (point))))))))


;;; Electric tree selection

(defvar ebrowse-electric-list-mode-map ()
  "Keymap used in electric Ebrowse buffer list window.")


(unless ebrowse-electric-list-mode-map
  (let ((map (make-keymap))
	(submap (make-keymap)))
    (setq ebrowse-electric-list-mode-map map)
    (fillarray (car (cdr map)) 'ebrowse-electric-list-undefined)
    (fillarray (car (cdr submap)) 'ebrowse-electric-list-undefined)
    (define-key map "\e" submap)
    (define-key map "\C-z" 'suspend-frame)
    (define-key map "\C-h" 'Helper-help)
    (define-key map "?" 'Helper-describe-bindings)
    (define-key map "\C-c" nil)
    (define-key map "\C-c\C-c" 'ebrowse-electric-list-quit)
    (define-key map "q" 'ebrowse-electric-list-quit)
    (define-key map " " 'ebrowse-electric-list-select)
    (define-key map "\C-l" 'recenter)
    (define-key map "\C-u" 'universal-argument)
    (define-key map "\C-p" 'previous-line)
    (define-key map "\C-n" 'next-line)
    (define-key map "p" 'previous-line)
    (define-key map "n" 'next-line)
    (define-key map "v" 'ebrowse-electric-view-buffer)
    (define-key map "\C-v" 'scroll-up)
    (define-key map "\ev" 'scroll-down)
    (define-key map "\e\C-v" 'scroll-other-window)
    (define-key map "\e>" 'end-of-buffer)
    (define-key map "\e<" 'beginning-of-buffer)
    (define-key map "\e>" 'end-of-buffer)))

(put 'ebrowse-electric-list-mode 'mode-class 'special)
(put 'ebrowse-electric-list-undefined 'suppress-keymap t)


(defun ebrowse-electric-list-mode ()
  "Mode for electric tree list mode."
  (kill-all-local-variables)
  (use-local-map ebrowse-electric-list-mode-map)
  (setq mode-name "Electric Position Menu"
	mode-line-buffer-identification "Electric Tree Menu")
  (when (memq 'mode-name mode-line-format)
    (setq mode-line-format (copy-sequence mode-line-format))
    (setcar (memq 'mode-name mode-line-format) "Tree Buffers"))
  (make-local-variable 'Helper-return-blurb)
  (setq Helper-return-blurb "return to buffer editing"
	truncate-lines t
	buffer-read-only t
	major-mode 'ebrowse-electric-list-mode)
  (run-mode-hooks 'ebrowse-electric-list-mode-hook))


(defun ebrowse-list-tree-buffers ()
  "Display a list of all tree buffers."
  (set-buffer (get-buffer-create "*Tree Buffers*"))
  (setq buffer-read-only nil)
  (erase-buffer)
  (insert "Tree\n" "----\n")
  (dolist (buffer (ebrowse-known-class-trees-buffer-list))
    (insert (buffer-name buffer) "\n"))
  (setq buffer-read-only t))


;;;###autoload
(defun ebrowse-electric-choose-tree ()
  "Return a buffer containing a tree or nil if no tree found or canceled."
  (interactive)
  (unless (car (ebrowse-known-class-trees-buffer-list))
    (error "No tree buffers"))
  (let (select buffer window)
    (save-window-excursion
      (save-window-excursion (ebrowse-list-tree-buffers))
      (setq window (Electric-pop-up-window "*Tree Buffers*")
	    buffer (window-buffer window))
      (shrink-window-if-larger-than-buffer window)
      (unwind-protect
	  (progn
	    (set-buffer buffer)
	    (ebrowse-electric-list-mode)
	    (setq select
		  (catch 'ebrowse-electric-list-select
		    (message "<<< Press Space to bury the list >>>")
		    (let ((first (progn (goto-char (point-min))
					(forward-line 2)
					(point)))
			  (last (progn (goto-char (point-max))
				       (forward-line -1)
				       (point)))
			  (goal-column 0))
		      (goto-char first)
		      (Electric-command-loop 'ebrowse-electric-list-select
					     nil
					     t
					     'ebrowse-electric-list-looper
					     (cons first last))))))
	(set-buffer buffer)
	(bury-buffer buffer)
	(message nil)))
    (when select
      (set-buffer buffer)
      (setq select (ebrowse-electric-get-buffer select)))
    (kill-buffer buffer)
    select))


(defun ebrowse-electric-list-looper (state condition)
  "Prevent cursor from moving beyond the buffer end.
Don't let it move into the title lines.
See 'Electric-command-loop' for a description of STATE and CONDITION."
  (cond ((and condition
	      (not (memq (car condition)
			 '(buffer-read-only end-of-buffer
					    beginning-of-buffer))))
	 (signal (car condition) (cdr condition)))
	((< (point) (car state))
	 (goto-char (point-min))
	 (forward-line 2))
	((> (point) (cdr state))
	 (goto-char (point-max))
	 (forward-line -1)
	 (if (pos-visible-in-window-p (point-max))
	     (recenter -1)))))


(defun ebrowse-electric-list-undefined ()
  "Function called for keys that are undefined."
  (interactive)
  (message "Type C-h for help, ? for commands, q to quit, Space to select.")
  (sit-for 4))


(defun ebrowse-electric-list-quit ()
  "Discard the buffer list."
  (interactive)
  (throw 'ebrowse-electric-list-select nil))


(defun ebrowse-electric-list-select ()
  "Select a buffer from the buffer list."
  (interactive)
  (throw 'ebrowse-electric-list-select (point)))


(defun ebrowse-electric-get-buffer (point)
  "Get a buffer corresponding to the line POINT is in."
  (let ((index (- (count-lines (point-min) point) 2)))
    (nth index (ebrowse-known-class-trees-buffer-list))))


;;; View a buffer for a tree.

(defun ebrowse-electric-view-buffer ()
  "View buffer point is on."
  (interactive)
  (let ((buffer (ebrowse-electric-get-buffer (point))))
    (cond (buffer
	   (view-buffer buffer))
	  (t
	   (error "Buffer no longer exists")))))


(defun ebrowse-choose-from-browser-buffers ()
  "Read a browser buffer name from the minibuffer and return that buffer."
  (let* ((buffers (ebrowse-known-class-trees-buffer-list)))
    (if buffers
	(if (not (second buffers))
	    (first buffers)
	  (or (ebrowse-electric-choose-tree) (error "No tree buffer")))
      (let* ((insert-default-directory t)
	     (file (read-file-name "Find tree: " nil nil t)))
	(save-excursion
	  (find-file file))
	(find-buffer-visiting file)))))


;;; Member buffers

(unless ebrowse-member-mode-map
  (let ((map (make-keymap)))
    (setf ebrowse-member-mode-map map)
    (suppress-keymap map)

  (when (display-mouse-p)
    (define-key map [down-mouse-3] 'ebrowse-member-mouse-3)
    (define-key map [mouse-2] 'ebrowse-member-mouse-2))

  (let ((map1 (make-sparse-keymap)))
    (suppress-keymap map1 t)
    (define-key map "C" map1)
    (define-key map1 "b" 'ebrowse-switch-member-buffer-to-base-class)
    (define-key map1 "c" 'ebrowse-switch-member-buffer-to-any-class)
    (define-key map1 "d" 'ebrowse-switch-member-buffer-to-derived-class)
    (define-key map1 "n" 'ebrowse-switch-member-buffer-to-next-sibling-class)
    (define-key map1 "p" 'ebrowse-switch-member-buffer-to-previous-sibling-class))

  (let ((map1 (make-sparse-keymap)))
    (suppress-keymap map1 t)
    (define-key map "D" map1)
    (define-key map1 "a" 'ebrowse-toggle-member-attributes-display)
    (define-key map1 "b" 'ebrowse-toggle-base-class-display)
    (define-key map1 "f" 'ebrowse-freeze-member-buffer)
    (define-key map1 "l" 'ebrowse-toggle-long-short-display)
    (define-key map1 "r" 'ebrowse-toggle-regexp-display)
    (define-key map1 "w" 'ebrowse-set-member-buffer-column-width))

  (let ((map1 (make-sparse-keymap)))
    (suppress-keymap map1 t)
    (define-key map "F" map1)
    (let ((map2 (make-sparse-keymap)))
      (suppress-keymap map2 t)
      (define-key map1 "a" map2)
      (define-key map2 "i" 'ebrowse-toggle-private-member-filter)
      (define-key map2 "o" 'ebrowse-toggle-protected-member-filter)
      (define-key map2 "u" 'ebrowse-toggle-public-member-filter))
    (define-key map1 "c" 'ebrowse-toggle-const-member-filter)
    (define-key map1 "i" 'ebrowse-toggle-inline-member-filter)
    (define-key map1 "p" 'ebrowse-toggle-pure-member-filter)
    (define-key map1 "r" 'ebrowse-remove-all-member-filters)
    (define-key map1 "v" 'ebrowse-toggle-virtual-member-filter))

  (let ((map1 (make-sparse-keymap)))
    (suppress-keymap map1 t)
    (define-key map "L" map1)
    (define-key map1 "d" 'ebrowse-display-friends-member-list)
    (define-key map1 "f" 'ebrowse-display-function-member-list)
    (define-key map1 "F" 'ebrowse-display-static-functions-member-list)
    (define-key map1 "n" 'ebrowse-display-next-member-list)
    (define-key map1 "p" 'ebrowse-display-previous-member-list)
    (define-key map1 "t" 'ebrowse-display-types-member-list)
    (define-key map1 "v" 'ebrowse-display-variables-member-list)
    (define-key map1 "V" 'ebrowse-display-static-variables-member-list))

  (let ((map1 (make-sparse-keymap)))
    (suppress-keymap map1 t)
    (define-key map "G" map1)
    (define-key map1 "m" 'ebrowse-goto-visible-member/all-member-lists)
    (define-key map1 "n" 'ebrowse-repeat-member-search)
    (define-key map1 "v" 'ebrowse-goto-visible-member))

  (define-key map "f" 'ebrowse-find-member-declaration)
  (define-key map "m" 'ebrowse-switch-to-next-member-buffer)
  (define-key map "q" 'bury-buffer)
  (define-key map "t" 'ebrowse-show-displayed-class-in-tree)
  (define-key map "v" 'ebrowse-view-member-declaration)
  (define-key map " " 'ebrowse-view-member-definition)
  (define-key map "?" 'describe-mode)
  (define-key map "\C-i" 'ebrowse-pop-from-member-to-tree-buffer)
  (define-key map "\C-l" 'ebrowse-redisplay-member-buffer)
  (define-key map "\C-m" 'ebrowse-find-member-definition)))



;;; Member mode

;;;###autoload
(defun ebrowse-member-mode ()
  "Major mode for Ebrowse member buffers.

\\{ebrowse-member-mode-map}"
  (kill-all-local-variables)
  (use-local-map ebrowse-member-mode-map)
  (setq major-mode 'ebrowse-member-mode)
  (mapc 'make-local-variable
	'(ebrowse--decl-column	        ;display column
	  ebrowse--n-columns		;number of short columns
	  ebrowse--column-width	        ;width of columns above
	  ebrowse--show-inherited-flag  ;include inherited members?
	  ebrowse--filters		;public, protected, private
	  ebrowse--accessor		;vars, functions, friends
	  ebrowse--displayed-class	;class displayed
	  ebrowse--long-display-flag	;display with regexps?
	  ebrowse--source-regexp-flag	;show source regexp?
	  ebrowse--attributes-flag	;show `virtual' and `inline'
	  ebrowse--member-list          ;list of members displayed
	  ebrowse--tree		        ;the class tree
	  ebrowse--member-mode-strings  ;part of mode line
	  ebrowse--tags-file-name	;
	  ebrowse--header
	  ebrowse--tree-obarray
	  ebrowse--virtual-display-flag
	  ebrowse--inline-display-flag
	  ebrowse--const-display-flag
	  ebrowse--pure-display-flag
	  ebrowse--frozen-flag))	;buffer not automagically reused
  (setq mode-name "Ebrowse-Members"
	mode-line-buffer-identification
	(propertized-buffer-identification "C++ Members")
	buffer-read-only t
	ebrowse--long-display-flag nil
	ebrowse--attributes-flag t
	ebrowse--show-inherited-flag t
	ebrowse--source-regexp-flag nil
	ebrowse--filters [0 1 2]
	ebrowse--decl-column ebrowse-default-declaration-column
	ebrowse--column-width ebrowse-default-column-width
	ebrowse--virtual-display-flag nil
	ebrowse--inline-display-flag nil
	ebrowse--const-display-flag nil
	ebrowse--pure-display-flag nil)
  (modify-syntax-entry ?_ (char-to-string (char-syntax ?a)))
  (run-mode-hooks 'ebrowse-member-mode-hook))



;;; Member mode mode line

(defsubst ebrowse-class-name-displayed-in-member-buffer ()
  "Return the name of the class displayed in the member buffer."
  (ebrowse-cs-name (ebrowse-ts-class ebrowse--displayed-class)))


(defsubst ebrowse-member-list-name ()
  "Return a string describing what is displayed in the member buffer."
  (get ebrowse--accessor (if (ebrowse-globals-tree-p ebrowse--displayed-class)
			     'ebrowse-global-title
			   'ebrowse-title)))


(defun ebrowse-update-member-buffer-mode-line ()
  "Update the mode line of member buffers."
  (let* ((name (when ebrowse--frozen-flag
		 (concat (ebrowse-class-name-displayed-in-member-buffer)
			 " ")))
	 (ident (concat name (ebrowse-member-list-name))))
    (setq mode-line-buffer-identification
	  (propertized-buffer-identification ident))
    (ebrowse-rename-buffer (if name ident ebrowse-member-buffer-name))
    (force-mode-line-update)))


;;; Misc member buffer commands

(defun ebrowse-freeze-member-buffer ()
  "Toggle frozen status of current buffer."
  (interactive)
  (setq ebrowse--frozen-flag (not ebrowse--frozen-flag))
  (ebrowse-redisplay-member-buffer))


(defun ebrowse-show-displayed-class-in-tree (arg)
  "Show the currently displayed class in the tree window.
With prefix ARG, switch to the tree buffer else pop to it."
  (interactive "P")
  (let ((class-name (ebrowse-class-name-displayed-in-member-buffer)))
    (when (ebrowse-pop-from-member-to-tree-buffer arg)
      (ebrowse-read-class-name-and-go class-name))))


(defun ebrowse-set-member-buffer-column-width ()
  "Set the column width of the member display.
The new width is read from the minibuffer."
  (interactive)
  (let ((width (string-to-number
		(read-from-minibuffer
		 (concat "Column width ("
			 (int-to-string (if ebrowse--long-display-flag
					    ebrowse--decl-column
					  ebrowse--column-width))
			 "): ")))))
    (when (plusp width)
      (if ebrowse--long-display-flag
	  (setq ebrowse--decl-column width)
	(setq ebrowse--column-width width))
      (ebrowse-redisplay-member-buffer))))


(defun ebrowse-pop-from-member-to-tree-buffer (arg)
  "Pop from a member buffer to the matching tree buffer.
Switch to the buffer if prefix ARG.  If no tree buffer exists,
make one."
  (interactive "P")
  (let ((buf (or (get-buffer (ebrowse-frozen-tree-buffer-name
			      ebrowse--tags-file-name))
		 (get-buffer ebrowse-tree-buffer-name)
		 (ebrowse-create-tree-buffer ebrowse--tree
					     ebrowse--tags-file-name
					     ebrowse--header
					     ebrowse--tree-obarray
					     'pop))))
    (and buf
	 (funcall (if arg 'switch-to-buffer 'pop-to-buffer) buf))
    buf))



;;; Switching between member lists

(defun ebrowse-display-member-list-for-accessor (accessor)
  "Switch the member buffer to display the member list for ACCESSOR."
  (setf ebrowse--accessor accessor
	ebrowse--member-list (funcall accessor ebrowse--displayed-class))
  (ebrowse-redisplay-member-buffer))


(defun ebrowse-cyclic-display-next/previous-member-list (incr)
  "Switch buffer to INCR'th next/previous list of members."
  (let ((index (ebrowse-position ebrowse--accessor
				 ebrowse-member-list-accessors)))
    (setf ebrowse--accessor
	  (cond ((plusp incr)
		 (or (nth (1+ index)
			  ebrowse-member-list-accessors)
		     (first ebrowse-member-list-accessors)))
		((minusp incr)
		 (or (and (>= (decf index) 0)
			  (nth index
			       ebrowse-member-list-accessors))
		     (first (last ebrowse-member-list-accessors))))))
    (ebrowse-display-member-list-for-accessor ebrowse--accessor)))


(defun ebrowse-display-next-member-list ()
  "Switch buffer to next member list."
  (interactive)
  (ebrowse-cyclic-display-next/previous-member-list 1))


(defun ebrowse-display-previous-member-list ()
  "Switch buffer to previous member list."
  (interactive)
  (ebrowse-cyclic-display-next/previous-member-list -1))


(defun ebrowse-display-function-member-list ()
  "Display the list of member functions."
  (interactive)
  (ebrowse-display-member-list-for-accessor 'ebrowse-ts-member-functions))


(defun ebrowse-display-variables-member-list ()
  "Display the list of member variables."
  (interactive)
  (ebrowse-display-member-list-for-accessor 'ebrowse-ts-member-variables))


(defun ebrowse-display-static-variables-member-list ()
  "Display the list of static member variables."
  (interactive)
  (ebrowse-display-member-list-for-accessor 'ebrowse-ts-static-variables))


(defun ebrowse-display-static-functions-member-list ()
  "Display the list of static member functions."
  (interactive)
  (ebrowse-display-member-list-for-accessor 'ebrowse-ts-static-functions))


(defun ebrowse-display-friends-member-list ()
  "Display the list of friends."
  (interactive)
  (ebrowse-display-member-list-for-accessor 'ebrowse-ts-friends))


(defun ebrowse-display-types-member-list ()
  "Display the list of types."
  (interactive)
  (ebrowse-display-member-list-for-accessor 'ebrowse-ts-types))



;;; Filters and other display attributes

(defun ebrowse-toggle-member-attributes-display ()
  "Toggle display of `virtual', `inline', `const' etc."
  (interactive)
  (setq ebrowse--attributes-flag (not ebrowse--attributes-flag))
  (ebrowse-redisplay-member-buffer))


(defun ebrowse-toggle-base-class-display ()
  "Toggle the display of members inherited from base classes."
  (interactive)
  (setf ebrowse--show-inherited-flag (not ebrowse--show-inherited-flag))
  (ebrowse-redisplay-member-buffer))


(defun ebrowse-toggle-pure-member-filter ()
  "Toggle display of pure virtual members."
  (interactive)
  (setf ebrowse--pure-display-flag (not ebrowse--pure-display-flag))
  (ebrowse-redisplay-member-buffer))


(defun ebrowse-toggle-const-member-filter ()
  "Toggle display of const members."
  (interactive)
  (setf ebrowse--const-display-flag (not ebrowse--const-display-flag))
  (ebrowse-redisplay-member-buffer))


(defun ebrowse-toggle-inline-member-filter ()
  "Toggle display of inline members."
  (interactive)
  (setf ebrowse--inline-display-flag (not ebrowse--inline-display-flag))
  (ebrowse-redisplay-member-buffer))


(defun ebrowse-toggle-virtual-member-filter ()
  "Toggle display of virtual members."
  (interactive)
  (setf ebrowse--virtual-display-flag (not ebrowse--virtual-display-flag))
  (ebrowse-redisplay-member-buffer))


(defun ebrowse-remove-all-member-filters ()
  "Remove all filters."
  (interactive)
  (dotimes (i 3)
    (aset ebrowse--filters i i))
  (setq ebrowse--pure-display-flag nil
	ebrowse--const-display-flag nil
	ebrowse--virtual-display-flag nil
	ebrowse--inline-display-flag nil)
  (ebrowse-redisplay-member-buffer))


(defun ebrowse-toggle-public-member-filter ()
  "Toggle visibility of public members."
  (interactive)
  (ebrowse-set-member-access-visibility 0)
  (ebrowse-redisplay-member-buffer))


(defun ebrowse-toggle-protected-member-filter ()
  "Toggle visibility of protected members."
  (interactive)
  (ebrowse-set-member-access-visibility 1)
  (ebrowse-redisplay-member-buffer))


(defun ebrowse-toggle-private-member-filter ()
  "Toggle visibility of private members."
  (interactive)
  (ebrowse-set-member-access-visibility 2)
  (ebrowse-redisplay-member-buffer))


(defun ebrowse-set-member-access-visibility (vis)
  (setf (aref ebrowse--filters vis)
	(if (aref ebrowse--filters vis) nil vis)))


(defun ebrowse-toggle-long-short-display ()
  "Toggle between long and short display form of member buffers."
  (interactive)
  (setf ebrowse--long-display-flag (not ebrowse--long-display-flag))
  (ebrowse-redisplay-member-buffer))


(defun ebrowse-toggle-regexp-display ()
  "Toggle declaration/definition regular expression display.
Used in member buffers showing the long display form."
  (interactive)
  (setf ebrowse--source-regexp-flag (not ebrowse--source-regexp-flag))
  (ebrowse-redisplay-member-buffer))



;;; Viewing/finding members

(defun ebrowse-find-member-definition (&optional prefix)
  "Find the file containing a member definition.
With PREFIX 4. find file in another window, with prefix 5
find file in another frame."
  (interactive "p")
  (ebrowse-view/find-member-declaration/definition prefix nil t))


(defun ebrowse-view-member-definition (prefix)
  "View the file containing a member definition.
With PREFIX 4. find file in another window, with prefix 5
find file in another frame."
  (interactive "p")
  (ebrowse-view/find-member-declaration/definition prefix t t))


(defun ebrowse-find-member-declaration (prefix)
  "Find the file containing a member's declaration.
With PREFIX 4. find file in another window, with prefix 5
find file in another frame."
  (interactive "p")
  (ebrowse-view/find-member-declaration/definition prefix nil))


(defun ebrowse-view-member-declaration (prefix)
  "View the file containing a member's declaration.
With PREFIX 4. find file in another window, with prefix 5
find file in another frame."
  (interactive "p")
  (ebrowse-view/find-member-declaration/definition prefix t))


(defun* ebrowse-view/find-member-declaration/definition
    (prefix view &optional definition info header tags-file-name)
  "Find or view a member declaration or definition.
With PREFIX 4. find file in another window, with prefix 5
find file in another frame.
DEFINITION non-nil means find the definition, otherwise find the
declaration.
INFO is a list (TREE ACCESSOR MEMBER) describing the member to
search.
TAGS-FILE-NAME is the file name of the BROWSE file."
  (unless header
    (setq header ebrowse--header))
  (unless tags-file-name
    (setq tags-file-name ebrowse--tags-file-name))
  (let (tree member accessor file on-class
	     (where (if (= prefix 4) 'other-window
		      (if (= prefix 5) 'other-frame 'this-window))))
    ;; If not given as parameters, get the necessary information
    ;; out of the member buffer.
    (if info
	(setq tree (first info)
	      accessor (second info)
	      member (third info))
      (multiple-value-setq (tree member on-class)
	(ebrowse-member-info-from-point))
      (setq accessor ebrowse--accessor))
    ;; View/find class if on a line containing a class name.
    (when on-class
      (return-from ebrowse-view/find-member-declaration/definition
	(ebrowse-view/find-file-and-search-pattern
	 (ebrowse-ts-class tree)
	 (list ebrowse--header (ebrowse-ts-class tree) nil)
	 (ebrowse-cs-file (ebrowse-ts-class tree))
	 tags-file-name view where)))
    ;; For some member lists, it doesn't make sense to search for
    ;; a definition. If this is requested, silently search for the
    ;; declaration.
    (when (and definition
	       (eq accessor 'ebrowse-ts-member-variables))
      (setq definition nil))
    ;; Construct a suitable `browse' struct for definitions.
    (when definition
      (setf member (make-ebrowse-ms
		    :name (ebrowse-ms-name member)
		    :file (ebrowse-ms-definition-file member)
		    :pattern (ebrowse-ms-definition-pattern
			      member)
		    :flags (ebrowse-ms-flags member)
		    :point (ebrowse-ms-definition-point
			    member))))
    ;; When no file information in member, use that of the class
    (setf file (or (ebrowse-ms-file member)
		   (if definition
		       (ebrowse-cs-source-file (ebrowse-ts-class tree))
		     (ebrowse-cs-file (ebrowse-ts-class tree)))))
    ;; When we have no regular expressions in the database the only
    ;; indication that the parser hasn't seen a definition/declaration
    ;; is that the search start point will be zero.
    (if (or (null file) (zerop (ebrowse-ms-point member)))
	(if (y-or-n-p (concat "No information about "
			      (if definition "definition" "declaration")
			      ".  Search for "
			      (if definition "declaration" "definition")
			      " of `"
			      (ebrowse-ms-name member)
			      "'? "))
	    (progn
	      (message nil)
	      ;; Recurse with new info.
	      (ebrowse-view/find-member-declaration/definition
	       prefix view (not definition) info header tags-file-name))
	  (error "Search canceled"))
      ;; Find that thing.
      (ebrowse-view/find-file-and-search-pattern
       (make-ebrowse-bs :name (ebrowse-ms-name member)
			:pattern (ebrowse-ms-pattern member)
			:file (ebrowse-ms-file member)
			:flags (ebrowse-ms-flags member)
			:point (ebrowse-ms-point member))
       (list header member accessor)
       file
       tags-file-name
       view
       where))))



;;; Drawing the member buffer

(defun ebrowse-redisplay-member-buffer ()
  "Force buffer redisplay."
  (interactive)
  (let ((display-fn (if ebrowse--long-display-flag
			'ebrowse-draw-member-long-fn
		      'ebrowse-draw-member-short-fn)))
    (ebrowse-output
      (erase-buffer)
      ;; Show this class
      (ebrowse-draw-member-buffer-class-line)
      (funcall display-fn ebrowse--member-list ebrowse--displayed-class)
      ;; Show inherited members if corresponding switch is on
      (when ebrowse--show-inherited-flag
	(dolist (super (ebrowse-base-classes ebrowse--displayed-class))
	  (goto-char (point-max))
	  (insert (if (bolp) "\n\n" "\n"))
	  (ebrowse-draw-member-buffer-class-line super)
	  (funcall display-fn (funcall ebrowse--accessor super) super)))
      (ebrowse-update-member-buffer-mode-line))))


(defun ebrowse-draw-member-buffer-class-line (&optional class)
  "Display the title line for a class section in the member buffer.
CLASS non-nil means display that class' title.  Otherwise use
the class cursor is on."
  (let ((start (point))
	(tree  (or class ebrowse--displayed-class))
	class-name-start
	class-name-end)
    (insert "class ")
    (setq class-name-start (point))
    (insert (ebrowse-qualified-class-name (ebrowse-ts-class tree)))
    (when (ebrowse-template-p (ebrowse-ts-class tree))
      (insert "<>"))
    (setq class-name-end (point))
    (insert ":\n\n")
    (ebrowse-set-face start (point) 'ebrowse-member-class)
    (add-text-properties
     class-name-start class-name-end
     '(ebrowse-what class-name
		    mouse-face highlight
		    help-echo "mouse-3: menu"))
    (put-text-property start class-name-end 'ebrowse-tree tree)))


(defun ebrowse-display-member-buffer (list &optional stand-alone class)
  "Start point for member buffer creation.
LIST is the member list to display.  STAND-ALONE non-nil
means the member buffer is standalone.  CLASS is its class."
  (let* ((classes ebrowse--tree-obarray)
	 (tree ebrowse--tree)
	 (tags-file-name ebrowse--tags-file-name)
	 (header ebrowse--header)
	 temp-buffer-setup-hook
	 (temp-buffer (get-buffer ebrowse-member-buffer-name)))
    ;; Get the class description from the name the cursor
    ;; is on if not specified as an argument.
    (unless class
      (setq class (ebrowse-tree-at-point)))
    (save-selected-window
      (if temp-buffer
	  (pop-to-buffer temp-buffer)
	(pop-to-buffer (get-buffer-create ebrowse-member-buffer-name))
	;; If new buffer, set the mode and initial values of locals
	(ebrowse-member-mode))
      ;; Set local variables
      (setq ebrowse--member-list (funcall list class)
	    ebrowse--displayed-class class
	    ebrowse--accessor list
	    ebrowse--tree-obarray classes
	    ebrowse--frozen-flag stand-alone
	    ebrowse--tags-file-name tags-file-name
	    ebrowse--header header
	    ebrowse--tree tree
	    buffer-read-only t)
      (ebrowse-redisplay-member-buffer)
      (current-buffer))))


(defun ebrowse-member-display-p (member)
  "Return t if MEMBER must be displayed under the current filter settings."
  (if (and (aref ebrowse--filters (ebrowse-ms-visibility member))
	   (or (null ebrowse--const-display-flag)
	       (ebrowse-const-p member))
	   (or (null ebrowse--inline-display-flag)
	       (ebrowse-inline-p member))
	   (or (null ebrowse--pure-display-flag)
	       (ebrowse-bs-p member))
	   (or (null ebrowse--virtual-display-flag)
	       (ebrowse-virtual-p member)))
      member))


(defun ebrowse-draw-member-attributes (member)
  "Insert a string for the attributes of MEMBER."
  (insert (if (ebrowse-template-p member) "T" "-")
	  (if (ebrowse-extern-c-p member) "C" "-")
	  (if (ebrowse-virtual-p member) "v" "-")
	  (if (ebrowse-inline-p member) "i" "-")
	  (if (ebrowse-const-p member) "c" "-")
	  (if (ebrowse-pure-virtual-p member) "0" "-")
	  (if (ebrowse-mutable-p member) "m" "-")
	  (if (ebrowse-explicit-p member) "e" "-")
	  (if (ebrowse-throw-list-p member) "t" "-")))


(defun ebrowse-draw-member-regexp (member-struc)
  "Insert a string for the regular expression matching MEMBER-STRUC."
  (let ((pattern (if ebrowse--source-regexp-flag
		     (ebrowse-ms-definition-pattern
		      member-struc)
		   (ebrowse-ms-pattern member-struc))))
    (cond ((stringp pattern)
	   (insert (ebrowse-trim-string pattern) "...\n")
	   (beginning-of-line 0)
	   (move-to-column (+ 4 ebrowse--decl-column))
	   (while (re-search-forward "[ \t]+" nil t)
	     (delete-region (match-beginning 0) (match-end 0))
	     (insert " "))
	   (beginning-of-line 2))
	  (t
	   (insert "[not recorded or unknown]\n")))))


(defun ebrowse-draw-member-long-fn (member-list tree)
  "Display member buffer for MEMBER-LIST in long form.
TREE is the class tree of MEMBER-LIST."
  (dolist (member-struc (mapcar 'ebrowse-member-display-p member-list))
    (when member-struc
      (let ((name (ebrowse-ms-name member-struc))
	    (start (point)))
	;; Insert member name truncated to the right length
	(insert (substring name
			   0
			   (min (length name)
				(1- ebrowse--decl-column))))
	(add-text-properties
	 start (point)
	 `(mouse-face highlight ebrowse-what member-name
		      ebrowse-member ,member-struc
		      ebrowse-tree ,tree
		      help-echo "mouse-2: view definition; mouse-3: menu"))
	;; Display virtual, inline, and const status
	(setf start (point))
	(indent-to ebrowse--decl-column)
	(put-text-property start (point) 'mouse-face nil)
	(when ebrowse--attributes-flag
	  (let ((start (point)))
	    (insert "<")
	    (ebrowse-draw-member-attributes member-struc)
	    (insert ">")
	    (ebrowse-set-face start (point)
			      'ebrowse-member-attribute)))
	(insert " ")
	(ebrowse-draw-member-regexp member-struc))))
  (insert "\n")
  (goto-char (point-min)))


(defun ebrowse-draw-member-short-fn (member-list tree)
  "Display MEMBER-LIST in short form.
TREE is the class tree in which the members are found."
  (let ((i 0)
	(column-width (+ ebrowse--column-width
			 (if ebrowse--attributes-flag 12 0))))
    ;; Get the number of columns to draw.
    (setq ebrowse--n-columns
	  (max 1 (/ (ebrowse-width-of-drawable-area) column-width)))
    (dolist (member (mapcar #'ebrowse-member-display-p member-list))
      (when member
	(let ((name (ebrowse-ms-name member))
	      start-of-entry
	      (start-of-column (point))
	      start-of-name)
	  (indent-to (* i column-width))
	  (put-text-property start-of-column (point) 'mouse-face nil)
	  (setq start-of-entry (point))
	  ;; Show various attributes
	  (when ebrowse--attributes-flag
	    (insert "<")
	    (ebrowse-draw-member-attributes member)
	    (insert "> ")
	    (ebrowse-set-face start-of-entry (point)
			      'ebrowse-member-attribute))
	  ;; insert member name truncated to column width
	  (setq start-of-name (point))
	  (insert (substring name 0
			     (min (length name)
				  (1- ebrowse--column-width))))
	  ;; set text properties
	  (add-text-properties
	   start-of-name (point)
	   `(ebrowse-what member-name
			  ebrowse-member ,member
			  mouse-face highlight
			  ebrowse-tree ,tree
			  help-echo "mouse-2: view definition; mouse-3: menu"))
	  (incf i)
	  (when (>= i ebrowse--n-columns)
	    (setf i 0)
	    (insert "\n")))))
    (when (plusp i)
      (insert "\n"))
    (goto-char (point-min))))



;;; Killing members from tree

(defun ebrowse-member-info-from-point ()
  "Ger information about the member at point.
The result has the form (TREE MEMBER NULL-P).  TREE is the tree
we're in, MEMBER is the member we're on.  NULL-P is t if MEMBER
is nil."
  (let ((tree (or (get-text-property (point) 'ebrowse-tree)
		  (error "No information at point")))
	(member (get-text-property (point) 'ebrowse-member)))
    (list tree member (null member))))



;;; Switching member buffer to display a selected member

(defun ebrowse-goto-visible-member/all-member-lists (prefix)
  "Position cursor on a member read from the minibuffer.
With PREFIX, search all members in the tree.  Otherwise consider
only members visible in the buffer."
  (interactive "p")
  (ebrowse-ignoring-completion-case
    (let* ((completion-list (ebrowse-name/accessor-alist-for-class-members))
	   (member (completing-read "Goto member: " completion-list nil t))
	   (accessor (cdr (assoc member completion-list))))
      (unless accessor
	(error "`%s' not found" member))
      (unless (eq accessor ebrowse--accessor)
	(setf ebrowse--accessor accessor
	      ebrowse--member-list (funcall accessor ebrowse--displayed-class))
	(ebrowse-redisplay-member-buffer))
      (ebrowse-move-point-to-member member))))


(defun ebrowse-goto-visible-member (repeat)
  "Position point on a member.
Read the member's name from the minibuffer.  Consider only members
visible in the member buffer.
REPEAT non-nil means repeat the search that number of times."
  (interactive "p")
  (ebrowse-ignoring-completion-case
    ;; Read member name
    (let* ((completion-list (ebrowse-name/accessor-alist-for-visible-members))
	   (member (completing-read "Goto member: " completion-list nil t)))
      (ebrowse-move-point-to-member member repeat))))



;;; Searching a member in the member buffer

(defun ebrowse-repeat-member-search (repeat)
  "Repeat the last regular expression search.
REPEAT, if specified, says repeat the search REPEAT times."
  (interactive "p")
  (unless ebrowse--last-regexp
    (error "No regular expression remembered"))
  ;; Skip over word the point is on
  (skip-chars-forward "^ \t\n")
  ;; Search for regexp from point
  (if (re-search-forward ebrowse--last-regexp nil t repeat)
      (progn
	(goto-char (match-beginning 0))
	(skip-chars-forward " \t\n"))
    ;; If not found above, repeat search from buffer start
    (goto-char (point-min))
    (if (re-search-forward ebrowse--last-regexp nil t)
	(progn
	  (goto-char (match-beginning 0))
	  (skip-chars-forward " \t\n"))
      (error "Not found"))))


(defun* ebrowse-move-point-to-member (name &optional count &aux member)
  "Set point on member NAME in the member buffer
COUNT, if specified, says search the COUNT'th member with the same name."
  (goto-char (point-min))
  (widen)
  (setq member
	(substring name 0 (min (length name) (1- ebrowse--column-width)))
	ebrowse--last-regexp
	(concat "[ \t\n]" (regexp-quote member) "[ \n\t]"))
  (if (re-search-forward ebrowse--last-regexp nil t count)
      (goto-char (1+ (match-beginning 0)))
    (error "Not found")))



;;; Switching member buffer to another class.

(defun ebrowse-switch-member-buffer-to-other-class (title compl-list)
  "Switch member buffer to a class read from the minibuffer.
Use TITLE as minibuffer prompt.
COMPL-LIST is a completion list to use."
  (let* ((initial (unless (second compl-list)
		    (first (first compl-list))))
	 (class (or (ebrowse-completing-read-value title compl-list initial)
		    (error "Not found"))))
    (setf ebrowse--displayed-class class
	  ebrowse--member-list (funcall ebrowse--accessor ebrowse--displayed-class))
    (ebrowse-redisplay-member-buffer)))


(defun ebrowse-switch-member-buffer-to-any-class ()
  "Switch member buffer to a class read from the minibuffer."
  (interactive)
  (ebrowse-switch-member-buffer-to-other-class
   "Goto class: " (ebrowse-tree-obarray-as-alist)))


(defun ebrowse-switch-member-buffer-to-base-class (arg)
  "Switch buffer to ARG'th base class."
  (interactive "P")
  (let ((supers (or (ebrowse-direct-base-classes ebrowse--displayed-class)
		    (error "No base classes"))))
    (if (and arg (second supers))
	(let ((alist (loop for s in supers
			   collect (cons (ebrowse-qualified-class-name
					  (ebrowse-ts-class s))
					 s))))
	  (ebrowse-switch-member-buffer-to-other-class
	   "Goto base class: " alist))
      (setq ebrowse--displayed-class (first supers)
	    ebrowse--member-list
	    (funcall ebrowse--accessor ebrowse--displayed-class))
      (ebrowse-redisplay-member-buffer))))

(defun ebrowse-switch-member-buffer-to-next-sibling-class (arg)
  "Move to ARG'th next sibling."
  (interactive "p")
  (ebrowse-switch-member-buffer-to-sibling-class arg))


(defun ebrowse-switch-member-buffer-to-previous-sibling-class (arg)
  "Move to ARG'th previous sibling."
  (interactive "p")
  (ebrowse-switch-member-buffer-to-sibling-class (- arg)))


(defun ebrowse-switch-member-buffer-to-sibling-class (inc)
  "Switch member display to nth sibling class.
Prefix arg INC specifies which one."
  (interactive "p")
  (let ((containing-list ebrowse--tree)
	index cls
	(supers (ebrowse-direct-base-classes ebrowse--displayed-class)))
    (flet ((trees-alist (trees)
			(loop for tr in trees
			      collect (cons (ebrowse-cs-name
					     (ebrowse-ts-class tr)) tr))))
      (when supers
	(let ((tree (if (second supers)
			(ebrowse-completing-read-value
			 "Relative to base class: "
			 (trees-alist supers) nil)
		      (first supers))))
	  (unless tree (error "Not found"))
	  (setq containing-list (ebrowse-ts-subclasses tree)))))
    (setq index (+ inc (ebrowse-position ebrowse--displayed-class
					 containing-list)))
    (cond ((minusp index) (message "No previous class"))
	  ((null (nth index containing-list)) (message "No next class")))
    (setq index (max 0 (min index (1- (length containing-list)))))
    (setq cls (nth index containing-list))
    (setf ebrowse--displayed-class cls
	  ebrowse--member-list (funcall ebrowse--accessor cls))
    (ebrowse-redisplay-member-buffer)))


(defun ebrowse-switch-member-buffer-to-derived-class (arg)
  "Switch member display to nth derived class.
Prefix arg ARG says which class should be displayed.  Default is
the first derived class."
  (interactive "P")
  (flet ((ebrowse-tree-obarray-as-alist ()
					(loop for s in (ebrowse-ts-subclasses
							ebrowse--displayed-class)
					      collect (cons (ebrowse-cs-name
							     (ebrowse-ts-class s)) s))))
    (let ((subs (or (ebrowse-ts-subclasses ebrowse--displayed-class)
		    (error "No derived classes"))))
      (if (and arg (second subs))
	  (ebrowse-switch-member-buffer-to-other-class
	   "Goto derived class: " (ebrowse-tree-obarray-as-alist))
	(setq ebrowse--displayed-class (first subs)
	      ebrowse--member-list
	      (funcall ebrowse--accessor ebrowse--displayed-class))
	(ebrowse-redisplay-member-buffer)))))



;;; Member buffer mouse functions

(defun ebrowse-displaying-functions ()
  (eq ebrowse--accessor 'ebrowse-ts-member-functions))
(defun ebrowse-displaying-variables ()
  (eq ebrowse--accessor 'ebrowse-ts-member-variables))
(defun ebrowse-displaying-static-functions ()
  )
(defun ebrowse-displaying-static-variables ()
  )
(defun ebrowse-displaying-types ()
  (eq ebrowse--accessor 'ebrowse-ts-types))
(defun ebrowse-displaying-friends ()
  (eq ebrowse--accessor 'ebrowse-ts-friends))

(easy-menu-define
 ebrowse-member-buffer-object-menu ebrowse-member-mode-map
 "Object menu for the member buffer itself."
 '("Members"
   ("Members List"
    ["Functions" ebrowse-display-function-member-list
     :help "Show the list of member functions"
     :style radio
     :selected (eq ebrowse--accessor 'ebrowse-ts-member-functions)
     :active t]
    ["Variables" ebrowse-display-variables-member-list
     :help "Show the list of member variables"
     :style radio
     :selected (eq ebrowse--accessor 'ebrowse-ts-member-variables)
     :active t]
    ["Static Functions" ebrowse-display-static-functions-member-list
     :help "Show the list of static member functions"
     :style radio
     :selected (eq ebrowse--accessor 'ebrowse-ts-static-functions)
     :active t]
    ["Static Variables" ebrowse-display-static-variables-member-list
     :help "Show the list of static member variables"
     :style radio
     :selected (eq ebrowse--accessor 'ebrowse-ts-static-variables)
     :active t]
    ["Types" ebrowse-display-types-member-list
     :help "Show the list of nested types"
     :style radio
     :selected (eq ebrowse--accessor 'ebrowse-ts-types)
     :active t]
    ["Friends/Defines" ebrowse-display-friends-member-list
     :help "Show the list of friends or defines"
     :style radio
     :selected (eq ebrowse--accessor 'ebrowse-ts-friends)
     :active t])
   ("Class"
    ["Up" ebrowse-switch-member-buffer-to-base-class
     :help "Show the base class of this class"
     :active t]
    ["Down" ebrowse-switch-member-buffer-to-derived-class
     :help "Show a derived class class of this class"
     :active t]
    ["Next Sibling" ebrowse-switch-member-buffer-to-next-sibling-class
     :help "Show the next sibling class"
     :active t]
    ["Previous Sibling" ebrowse-switch-member-buffer-to-previous-sibling-class
     :help "Show the previous sibling class"
     :active t])
   ("Member"
    ["Show in Tree" ebrowse-show-displayed-class-in-tree
     :help "Show this class in the class tree"
     :active t]
    ["Find in this Class" ebrowse-goto-visible-member
     :help "Search for a member of this class"
     :active t]
    ["Find in Tree" ebrowse-goto-visible-member/all-member-lists
     :help "Search for a member in any class"
     :active t])
   ("Display"
    ["Inherited" ebrowse-toggle-base-class-display
     :help "Toggle display of inherited members"
     :style toggle
     :selected ebrowse--show-inherited-flag
     :active t]
    ["Attributes" ebrowse-toggle-member-attributes-display
     :help "Show member attributes"
     :style toggle
     :selected ebrowse--attributes-flag
     :active t]
    ["Long Display" ebrowse-toggle-long-short-display
     :help "Toggle the member display format"
     :style toggle
     :selected ebrowse--long-display-flag
     :active t]
    ["Column Width" ebrowse-set-member-buffer-column-width
     :help "Set the display's column width"
     :active t])
   ("Filter"
    ["Public" ebrowse-toggle-public-member-filter
     :help "Toggle the visibility of public members"
     :style toggle
     :selected (not (aref ebrowse--filters 0))
     :active t]
    ["Protected" ebrowse-toggle-protected-member-filter
     :help "Toggle the visibility of protected members"
     :style toggle
     :selected (not (aref ebrowse--filters 1))
     :active t]
    ["Private" ebrowse-toggle-private-member-filter
     :help "Toggle the visibility of private members"
     :style toggle
     :selected (not (aref ebrowse--filters 2))
     :active t]
    ["Virtual" ebrowse-toggle-virtual-member-filter
     :help "Toggle the visibility of virtual members"
     :style toggle
     :selected ebrowse--virtual-display-flag
     :active t]
    ["Inline" ebrowse-toggle-inline-member-filter
     :help "Toggle the visibility of inline members"
     :style toggle
     :selected ebrowse--inline-display-flag
     :active t]
    ["Const" ebrowse-toggle-const-member-filter
     :help "Toggle the visibility of const members"
     :style toggle
     :selected ebrowse--const-display-flag
     :active t]
    ["Pure" ebrowse-toggle-pure-member-filter
     :help "Toggle the visibility of pure virtual members"
     :style toggle
     :selected ebrowse--pure-display-flag
     :active t]
    "-----------------"
    ["Show all" ebrowse-remove-all-member-filters
     :help "Remove any display filters"
     :active t])
   ("Buffer"
    ["Tree" ebrowse-pop-from-member-to-tree-buffer
     :help "Pop to the class tree buffer"
     :active t]
    ["Next Member Buffer" ebrowse-switch-to-next-member-buffer
     :help "Switch to the next member buffer of this class tree"
     :active t]
    ["Freeze" ebrowse-freeze-member-buffer
     :help "Freeze (do not reuse) this member buffer"
     :active t])))


(defun ebrowse-on-class-name ()
  "Value is non-nil if point is on a class name."
  (eq (get-text-property (point) 'ebrowse-what) 'class-name))


(defun ebrowse-on-member-name ()
  "Value is non-nil if point is on a member name."
  (eq (get-text-property (point) 'ebrowse-what) 'member-name))


(easy-menu-define
 ebrowse-member-class-name-object-menu ebrowse-member-mode-map
 "Object menu for class names in member buffer."
 '("Class"
   ["Find" ebrowse-find-member-definition
    :help "Find this class in the source files"
    :active (eq (get-text-property (point) 'ebrowse-what) 'class-name)]
   ["View" ebrowse-view-member-definition
    :help "View this class in the source files"
    :active (eq (get-text-property (point) 'ebrowse-what) 'class-name)]))


(easy-menu-define
 ebrowse-member-name-object-menu ebrowse-member-mode-map
 "Object menu for member names"
 '("Ebrowse"
   ["Find Definition" ebrowse-find-member-definition
    :help "Find this member's definition in the source files"
    :active (ebrowse-on-member-name)]
   ["Find Declaration" ebrowse-find-member-declaration
    :help "Find this member's declaration in the source files"
    :active (ebrowse-on-member-name)]
   ["View Definition" ebrowse-view-member-definition
    :help "View this member's definition in the source files"
    :active (ebrowse-on-member-name)]
   ["View Declaration" ebrowse-view-member-declaration
    :help "View this member's declaration in the source files"
    :active (ebrowse-on-member-name)]))


(defun ebrowse-member-mouse-3 (event)
  "Handle `mouse-3' events in member buffers.
EVENT is the mouse event."
  (interactive "e")
  (mouse-set-point event)
  (case (event-click-count event)
    (2 (ebrowse-find-member-definition))
    (1 (case (get-text-property (posn-point (event-start event))
				'ebrowse-what)
	 (member-name
	  (ebrowse-popup-menu ebrowse-member-name-object-menu event))
	 (class-name
	  (ebrowse-popup-menu ebrowse-member-class-name-object-menu event))
	 (t
	  (ebrowse-popup-menu ebrowse-member-buffer-object-menu event))))))


(defun ebrowse-member-mouse-2 (event)
  "Handle `mouse-2' events in member buffers.
EVENT is the mouse event."
  (interactive "e")
  (mouse-set-point event)
  (case (event-click-count event)
    (2 (ebrowse-find-member-definition))
    (1 (case (get-text-property (posn-point (event-start event))
				'ebrowse-what)
	 (member-name
	  (ebrowse-view-member-definition 0))))))



;;; Tags view/find

(defun ebrowse-class-alist-for-member (tree-header name)
  "Return information about a member in a class tree.
TREE-HEADER is the header structure of the class tree.
NAME is the name of the member.
Value is an alist of elements (CLASS-NAME . (CLASS LIST NAME)),
where each element describes one occurrence of member NAME in the tree.
CLASS-NAME is the qualified name of the class in which the
member was found.  The CDR of the acons is described in function
`ebrowse-class/index/member-for-member'."
  (let ((table (ebrowse-member-table tree-header))
	known-classes
	alist)
    (when name
      (dolist (info (gethash name table) alist)
	(unless (memq (first info) known-classes)
	  (setf alist (acons (ebrowse-qualified-class-name
			      (ebrowse-ts-class (first info)))
			     info alist)
		known-classes (cons (first info) known-classes)))))))


(defun ebrowse-choose-tree ()
  "Choose a class tree to use.
If there's more than one class tree loaded, let the user choose
the one he wants.  Value is (TREE HEADER BUFFER), with TREE being
the class tree, HEADER the header structure of the tree, and BUFFER
being the tree or member buffer containing the tree."
  (let* ((buffer (ebrowse-choose-from-browser-buffers)))
    (if buffer (list (ebrowse-value-in-buffer 'ebrowse--tree buffer)
		     (ebrowse-value-in-buffer 'ebrowse--header buffer)
		     buffer))))


(defun ebrowse-tags-read-name (header prompt)
  "Read a C++ identifier from the minibuffer.
HEADER is the `ebrowse-hs' structure of the class tree.
Prompt with PROMPT.  Insert into the minibuffer a C++ identifier read
from point as default.  Value is a list (CLASS-NAME MEMBER-NAME)."
  (save-excursion
    (let* (start member-info (members (ebrowse-member-table header)))
      (multiple-value-bind (class-name member-name)
	  (ebrowse-tags-read-member+class-name)
	(unless member-name
	  (error "No member name at point"))
	(if members
	    (let* ((name (ebrowse-ignoring-completion-case
			   (completing-read prompt members nil nil member-name)))
		   (completion-result (try-completion name members)))
	      ;; Cannot rely on `try-completion' returning t for exact
	      ;; matches!  It returns the name as a string.
	      (unless (setq member-info (gethash name members))
		(if (y-or-n-p "No exact match found.  Try substrings? ")
		    (setq name
			  (or (first (ebrowse-list-of-matching-members
				      members (regexp-quote name) name))
			      (error "Sorry, nothing found")))
		  (error "Canceled")))
	      (list class-name name))
	  (list class-name (read-from-minibuffer prompt member-name)))))))


(defun ebrowse-tags-read-member+class-name ()
  "Read a C++ identifier from point.
Value is (CLASS-NAME MEMBER-NAME).
CLASS-NAME is the name of the class if the identifier was qualified.
It is nil otherwise.
MEMBER-NAME is the name of the member found."
  (save-excursion
    (skip-chars-backward "a-zA-Z0-9_")
    (let* ((start (point))
	   (name (progn (skip-chars-forward "a-zA-Z0-9_")
			(buffer-substring start (point))))
	   class)
      (list class name))))


(defun ebrowse-tags-choose-class (tree header name initial-class-name)
  "Read a class name for a member from the minibuffer.
TREE is the class tree we operate on.
HEADER is its header structure.
NAME is the name of the member.
INITIAL-CLASS-NAME is an initial class name to insert in the minibuffer.
Value is a list (TREE ACCESSOR MEMBER) for the member."
  (let ((alist (or (ebrowse-class-alist-for-member header name)
		   (error "No classes with member `%s' found" name))))
    (ebrowse-ignoring-completion-case
      (if (null (second alist))
	  (cdr (first alist))
	(push ?\? unread-command-events)
	(cdr (assoc (completing-read "In class: "
				     alist nil t initial-class-name)
		    alist))))))


(defun* ebrowse-tags-view/find-member-decl/defn
    (prefix &key view definition member-name)
  "If VIEW is t, view, else find an occurrence of MEMBER-NAME.

If DEFINITION is t, find or view the member definition else its
declaration.  This function reads the member's name from the
current buffer like FIND-TAG.  It then prepares a completion list
of all classes containing a member with the given name and lets
the user choose the class to use.  As a last step, a tags search
is performed that positions point on the member declaration or
definition."
  (multiple-value-bind
      (tree header tree-buffer) (ebrowse-choose-tree)
    (unless tree (error "No class tree"))
    (let* ((marker (point-marker))
	   class-name
	   (name member-name)
	   info)
      (unless name
	(multiple-value-setq (class-name name)
	  (ebrowse-tags-read-name
	   header
	   (concat (if view "View" "Find") " member "
		   (if definition "definition" "declaration") ": "))))
      (setq info (ebrowse-tags-choose-class tree header name class-name))
      (ebrowse-push-position marker info)
      ;; Goto the occurrence of the member
      (ebrowse-view/find-member-declaration/definition
       prefix view definition info
       header
       (ebrowse-value-in-buffer 'ebrowse--tags-file-name tree-buffer))
      ;; Record position jumped to
      (ebrowse-push-position (point-marker) info t))))


;;;###autoload
(defun ebrowse-tags-view-declaration ()
  "View declaration of member at point."
  (interactive)
  (ebrowse-tags-view/find-member-decl/defn 0 :view t :definition nil))


;;;###autoload
(defun ebrowse-tags-find-declaration ()
  "Find declaration of member at point."
  (interactive)
  (ebrowse-tags-view/find-member-decl/defn 0 :view nil :definition nil))


;;;###autoload
(defun ebrowse-tags-view-definition ()
  "View definition of member at point."
  (interactive)
  (ebrowse-tags-view/find-member-decl/defn 0 :view t :definition t))


;;;###autoload
(defun ebrowse-tags-find-definition ()
  "Find definition of member at point."
  (interactive)
  (ebrowse-tags-view/find-member-decl/defn 0 :view nil :definition t))


(defun ebrowse-tags-view-declaration-other-window ()
  "View declaration of member at point in other window."
  (interactive)
  (ebrowse-tags-view/find-member-decl/defn 4 :view t :definition nil))


;;;###autoload
(defun ebrowse-tags-find-declaration-other-window ()
  "Find declaration of member at point in other window."
  (interactive)
  (ebrowse-tags-view/find-member-decl/defn 4 :view nil :definition nil))


;;;###autoload
(defun ebrowse-tags-view-definition-other-window ()
  "View definition of member at point in other window."
  (interactive)
  (ebrowse-tags-view/find-member-decl/defn 4 :view t :definition t))


;;;###autoload
(defun ebrowse-tags-find-definition-other-window ()
  "Find definition of member at point in other window."
  (interactive)
  (ebrowse-tags-view/find-member-decl/defn 4 :view nil :definition t))


(defun ebrowse-tags-view-declaration-other-frame ()
  "View definition of member at point in other frame."
  (interactive)
  (ebrowse-tags-view/find-member-decl/defn 5 :view t :definition nil))


;;;###autoload
(defun ebrowse-tags-find-declaration-other-frame ()
  "Find definition of member at point in other frame."
  (interactive)
  (ebrowse-tags-view/find-member-decl/defn 5 :view nil :definition nil))


;;;###autoload
(defun ebrowse-tags-view-definition-other-frame ()
  "View definition of member at point in other frame."
  (interactive)
  (ebrowse-tags-view/find-member-decl/defn 5 :view t :definition t))


;;;###autoload
(defun ebrowse-tags-find-definition-other-frame ()
  "Find definition of member at point in other frame."
  (interactive)
  (ebrowse-tags-view/find-member-decl/defn 5 :view nil :definition t))


(defun ebrowse-tags-select/create-member-buffer (tree-buffer info)
  "Select or create member buffer.
TREE-BUFFER specifies the tree to use.  INFO describes the member.
It is a list (TREE ACCESSOR MEMBER)."
  (let ((buffer (get-buffer ebrowse-member-buffer-name)))
    (cond ((null buffer)
	   (set-buffer tree-buffer)
	   (switch-to-buffer (ebrowse-display-member-buffer
			      (second info) nil (first info))))
	  (t
	   (switch-to-buffer buffer)
	   (setq ebrowse--displayed-class (first info)
		 ebrowse--accessor (second info)
		 ebrowse--member-list (funcall ebrowse--accessor ebrowse--displayed-class))
	   (ebrowse-redisplay-member-buffer)))
    (ebrowse-move-point-to-member (ebrowse-ms-name (third info)))))


(defun ebrowse-tags-display-member-buffer (&optional fix-name)
  "Display a member buffer for a member.
FIX-NAME non-nil means display the buffer for that member.
Otherwise read a member name from point."
  (interactive)
  (multiple-value-bind
      (tree header tree-buffer) (ebrowse-choose-tree)
    (unless tree (error "No class tree"))
    (let* ((marker (point-marker)) class-name (name fix-name) info)
      (unless name
	(multiple-value-setq (class-name name)
	  (ebrowse-tags-read-name header
				  (concat "Find member list of: "))))
      (setq info (ebrowse-tags-choose-class tree header name class-name))
      (ebrowse-push-position marker info)
      (ebrowse-tags-select/create-member-buffer tree-buffer info))))


(defun ebrowse-list-of-matching-members (members regexp &optional name)
  "Return a list of members in table MEMBERS matching REGEXP or NAME.
Both NAME and REGEXP may be nil in which case exact or regexp matches
are not performed."
  (let (list)
    (when (or name regexp)
      (maphash #'(lambda (member-name info)
		   (when (or (and name (string= name member-name))
			     (and regexp (string-match regexp member-name)))
		     (setq list (cons member-name list))))
	       members))
    list))


(defun ebrowse-tags-apropos ()
  "Display a list of members matching a regexp read from the minibuffer."
  (interactive)
  (let* ((buffer (or (ebrowse-choose-from-browser-buffers)
		     (error "No tree buffer")))
	 (header (ebrowse-value-in-buffer 'ebrowse--header buffer))
	 (members (ebrowse-member-table header))
	 temp-buffer-setup-hook
	 (regexp (read-from-minibuffer "List members matching regexp: ")))
    (with-output-to-temp-buffer (concat "*Apropos Members*")
      (set-buffer standard-output)
      (erase-buffer)
      (insert "Members matching `" regexp "'\n\n")
      (loop for s in (ebrowse-list-of-matching-members members regexp) do
	    (loop for info in (gethash s members) do
		  (ebrowse-draw-file-member-info info))))))


(defun ebrowse-tags-list-members-in-file ()
  "Display a list of members found in a file.
The file name is read from the minibuffer."
  (interactive)
  (let* ((buffer (or (ebrowse-choose-from-browser-buffers)
		     (error "No tree buffer")))
	 (files (save-excursion (set-buffer buffer) (ebrowse-files-table)))
	 (file (completing-read "List members in file: " files nil t))
	 (header (ebrowse-value-in-buffer 'ebrowse--header buffer))
	 temp-buffer-setup-hook
	 (members (ebrowse-member-table header)))
    (with-output-to-temp-buffer (concat "*Members in file " file "*")
      (set-buffer standard-output)
      (maphash
       #'(lambda (member-name list)
	   (loop for info in list
		 as member = (third info)
		 as class = (ebrowse-ts-class (first info))
		 when (or (and (null (ebrowse-ms-file member))
			       (string= (ebrowse-cs-file class) file))
			  (string= file (ebrowse-ms-file member)))
		 do (ebrowse-draw-file-member-info info "decl.")
		 when (or (and (null (ebrowse-ms-definition-file member))
			       (string= (ebrowse-cs-source-file class) file))
			  (string= file (ebrowse-ms-definition-file member)))
		 do (ebrowse-draw-file-member-info info "defn.")))
       members))))


(defun* ebrowse-draw-file-member-info (info &optional (kind ""))
  "Display a line in the members info buffer.
INFO describes the member.  It has the form (TREE ACCESSOR MEMBER).
TREE is the class of the member to display.
ACCESSOR is the accessor symbol of its member list.
MEMBER is the member structure.
KIND is an additional string printed in the buffer."
  (let* ((tree (first info))
	 (globals-p (ebrowse-globals-tree-p tree)))
    (unless globals-p
      (insert (ebrowse-cs-name (ebrowse-ts-class tree))))
    (insert "::" (ebrowse-ms-name (third info)))
    (indent-to 40)
    (insert kind)
    (indent-to 50)
    (insert (case (second info)
	      ('ebrowse-ts-member-functions "member function")
	      ('ebrowse-ts-member-variables "member variable")
	      ('ebrowse-ts-static-functions "static function")
	      ('ebrowse-ts-static-variables "static variable")
	      ('ebrowse-ts-friends (if globals-p "define" "friend"))
	      ('ebrowse-ts-types "type")
	      (t "unknown"))
	    "\n")))

(defvar ebrowse-last-completion nil
  "Text inserted by the last completion operation.")


(defvar ebrowse-last-completion-start nil
  "String which was the basis for the last completion operation.")


(defvar ebrowse-last-completion-location nil
  "Buffer position at which the last completion operation was initiated.")


(defvar ebrowse-last-completion-obarray nil
  "Member used in last completion operation.")


(make-variable-buffer-local 'ebrowse-last-completion-obarray)
(make-variable-buffer-local 'ebrowse-last-completion-location)
(make-variable-buffer-local 'ebrowse-last-completion)
(make-variable-buffer-local 'ebrowse-last-completion-start)



(defun ebrowse-some-member-table ()
  "Return a hash table containing all members of a tree.
If there's only one tree loaded, use that.  Otherwise let the
use choose a tree."
  (let* ((buffers (ebrowse-known-class-trees-buffer-list))
	 (buffer (cond ((and (first buffers) (not (second buffers)))
			(first buffers))
		       (t (or (ebrowse-electric-choose-tree)
			      (error "No tree buffer")))))
	 (header (ebrowse-value-in-buffer 'ebrowse--header buffer)))
    (ebrowse-member-table header)))


(defun ebrowse-cyclic-successor-in-string-list (string list)
  "Return the item following STRING in LIST.
If STRING is the last element, return the first element as successor."
  (or (nth (1+ (ebrowse-position string list 'string=)) list)
      (first list)))


;;; Symbol completion

;;;###autoload
(defun* ebrowse-tags-complete-symbol (prefix)
  "Perform completion on the C++ symbol preceding point.
A second call of this function without changing point inserts the next match.
A call with prefix PREFIX reads the symbol to insert from the minibuffer with
completion."
  (interactive "P")
  (let* ((end (point))
	 (begin (save-excursion (skip-chars-backward "a-zA-Z_0-9") (point)))
	 (pattern (buffer-substring begin end))
	 list completion)
    (cond
     ;; With prefix, read name from minibuffer with completion.
     (prefix
      (let* ((members (ebrowse-some-member-table))
	     (completion (completing-read "Insert member: "
					  members nil t pattern)))
	(when completion
	  (setf ebrowse-last-completion-location nil)
	  (delete-region begin end)
	  (insert completion))))
     ;; If this function is called at the same point the last
     ;; expansion ended, insert the next expansion.
     ((eq (point) ebrowse-last-completion-location)
      (setf list (all-completions ebrowse-last-completion-start
				  ebrowse-last-completion-obarray)
	    completion (ebrowse-cyclic-successor-in-string-list
			ebrowse-last-completion list))
      (cond ((null completion)
	     (error "No completion"))
	    ((string= completion pattern)
	     (error "No further completion"))
	    (t
	     (delete-region begin end)
	     (insert completion)
	     (setf ebrowse-last-completion completion
		   ebrowse-last-completion-location (point)))))
     ;; First time the function is called at some position in the
     ;; buffer: Start new completion.
     (t
      (let* ((members (ebrowse-some-member-table))
	     (completion (first (all-completions pattern members nil))))
	(cond ((eq completion t))
	      ((null completion)
	       (error "Can't find completion for `%s'" pattern))
	      (t
	       (delete-region begin end)
	       (insert completion)

	       (setf ebrowse-last-completion-location (point)
		     ebrowse-last-completion-start pattern
		     ebrowse-last-completion completion
		     ebrowse-last-completion-obarray members))))))))


;;; Tags query replace & search

(defvar ebrowse-tags-loop-form ()
  "Form for `ebrowse-loop-continue'.
Evaluated for each file in the tree.  If it returns nil, proceed
with the next file.")

(defvar ebrowse-tags-next-file-list ()
  "A list of files to be processed.")


(defvar ebrowse-tags-next-file-path nil
  "The path relative to which files have to be searched.")


(defvar ebrowse-tags-loop-last-file nil
  "The last file visited via `ebrowse-tags-loop'.")


(defun ebrowse-tags-next-file (&optional initialize tree-buffer)
  "Select next file among files in current tag table.
Non-nil argument INITIALIZE (prefix arg, if interactive) initializes
to the beginning of the list of files in the tag table.
TREE-BUFFER specifies the class tree we operate on."
  (interactive "P")
  ;; Call with INITIALIZE non-nil initializes the files list.
  ;; If more than one tree buffer is loaded, let the user choose
  ;; on which tree (s)he wants to operate.
  (when initialize
    (let ((buffer (or tree-buffer (ebrowse-choose-from-browser-buffers))))
      (save-excursion
	(set-buffer buffer)
	(setq ebrowse-tags-next-file-list
	      (ebrowse-files-list (ebrowse-marked-classes-p))
	      ebrowse-tags-loop-last-file
	      nil
	      ebrowse-tags-next-file-path
	      (file-name-directory ebrowse--tags-file-name)))))
  ;; End of the loop if the stack of files is empty.
  (unless ebrowse-tags-next-file-list
    (error "All files processed"))
  ;; ebrowse-tags-loop-last-file is the last file that was visited due
  ;; to a call to BROWSE-LOOP (see below). If that file is still
  ;; in memory, and it wasn't modified, throw its buffer away to
  ;; prevent cluttering up the buffer list.
  (when ebrowse-tags-loop-last-file
    (let ((buffer (get-file-buffer ebrowse-tags-loop-last-file)))
      (when (and buffer
		 (not (buffer-modified-p buffer)))
	(kill-buffer buffer))))
  ;; Remember this buffer file name for later deletion, if it
  ;; wasn't visited by other means.
  (let ((file (expand-file-name (car ebrowse-tags-next-file-list)
				ebrowse-tags-next-file-path)))
    (setq ebrowse-tags-loop-last-file (if (get-file-buffer file) nil file))
    ;; Find the file and pop the file list. Pop has to be done
    ;; before the file is loaded because FIND-FILE might encounter
    ;; an error, and we want to be able to proceed with the next
    ;; file in this case.
    (pop ebrowse-tags-next-file-list)
    (find-file file)))


;;;###autoload
(defun ebrowse-tags-loop-continue (&optional first-time tree-buffer)
  "Repeat last operation on files in tree.
FIRST-TIME non-nil means this is not a repetition, but the first time.
TREE-BUFFER if indirectly specifies which files to loop over."
  (interactive)
  (when first-time
    (ebrowse-tags-next-file first-time tree-buffer)
    (goto-char (point-min)))
  (while (not (eval ebrowse-tags-loop-form))
    (ebrowse-tags-next-file)
    (message "Scanning file `%s'..." buffer-file-name)
    (goto-char (point-min))))


;;;###autoload
(defun ebrowse-tags-search (regexp)
  "Search for REGEXP in all files in a tree.
If marked classes exist, process marked classes, only.
If regular expression is nil, repeat last search."
  (interactive "sTree search (regexp): ")
  (if (and (string= regexp "")
	   (eq (car ebrowse-tags-loop-form) 're-search-forward))
      (ebrowse-tags-loop-continue)
    (setq ebrowse-tags-loop-form (list 're-search-forward regexp nil t))
    (ebrowse-tags-loop-continue 'first-time)))


;;;###autoload
(defun ebrowse-tags-query-replace (from to)
  "Query replace FROM with TO in all files of a class tree.
With prefix arg, process files of marked classes only."
  (interactive
   "sTree query replace (regexp): \nsTree query replace %s by: ")
  (setq ebrowse-tags-loop-form
	(list 'and (list 'save-excursion
			 (list 're-search-forward from nil t))
	      (list 'not (list 'perform-replace from to t t nil))))
  (ebrowse-tags-loop-continue 'first-time))


;;;###autoload
(defun ebrowse-tags-search-member-use (&optional fix-name)
  "Search for call sites of a member.
If FIX-NAME is specified, search uses of that member.
Otherwise, read a member name from the minibuffer.
Searches in all files mentioned in a class tree for something that
looks like a function call to the member."
  (interactive)
  ;; Choose the tree to use if there is more than one.
  (multiple-value-bind (tree header tree-buffer)
      (ebrowse-choose-tree)
    (unless tree
      (error "No class tree"))
    ;; Get the member name NAME (class-name is ignored).
    (let ((name fix-name) class-name regexp)
      (unless name
	(multiple-value-setq (class-name name)
	  (ebrowse-tags-read-name header "Find calls of: ")))
      ;; Set tags loop form to search for member and begin loop.
      (setq regexp (concat "\\<" name "[ \t]*(")
	    ebrowse-tags-loop-form (list 're-search-forward regexp nil t))
      (ebrowse-tags-loop-continue 'first-time tree-buffer))))



;;; Tags position management

;;; Structures of this kind are the elements of the position stack.

(defstruct (ebrowse-position (:type vector) :named)
  file-name				; in which file
  point					; point in file
  target				; t if target of a jump
  info)					; (CLASS FUNC MEMBER) jumped to


(defvar ebrowse-position-stack ()
  "Stack of `ebrowse-position' structured.")


(defvar ebrowse-position-index 0
  "Current position in position stack.")


(defun ebrowse-position-name (position)
  "Return an identifying string for POSITION.
The string is printed in the electric position list buffer."
  (let ((info (ebrowse-position-info position)))
    (concat (if (ebrowse-position-target position) "at " "to ")
	    (ebrowse-cs-name (ebrowse-ts-class (first info)))
	    "::" (ebrowse-ms-name (third info)))))


(defun ebrowse-view/find-position (position &optional view)
  "Position point on POSITION.
If VIEW is non-nil, view the position, otherwise find it."
  (cond ((not view)
	 (find-file (ebrowse-position-file-name position))
	 (goto-char (ebrowse-position-point position)))
	(t
	 (unwind-protect
	     (progn
	       (push (function
		      (lambda ()
			(goto-char (ebrowse-position-point position))))
		     view-mode-hook)
	       (view-file (ebrowse-position-file-name position)))
	   (pop view-mode-hook)))))


(defun ebrowse-push-position (marker info &optional target)
  "Push current position on position stack.
MARKER is the marker to remember as position.
INFO is a list (CLASS FUNC MEMBER) specifying what we jumped to.
TARGET non-nil means we performed a jump.
Positions in buffers that have no file names are not saved."
  (when (buffer-file-name (marker-buffer marker))
    (let ((too-much (- (length ebrowse-position-stack)
		       ebrowse-max-positions)))
      ;; Do not let the stack grow to infinity.
      (when (plusp too-much)
	(setq ebrowse-position-stack
	      (butlast ebrowse-position-stack too-much)))
      ;; Push the position.
      (push (make-ebrowse-position
	     :file-name (buffer-file-name (marker-buffer marker))
	     :point (marker-position marker)
	     :target target
	     :info info)
	    ebrowse-position-stack))))


(defun ebrowse-move-in-position-stack (increment)
  "Move by INCREMENT in the position stack."
  (let ((length (length ebrowse-position-stack)))
    (when (zerop length)
      (error "No positions remembered"))
    (setq ebrowse-position-index
	  (mod (+ increment ebrowse-position-index) length))
    (message "Position %d of %d " ebrowse-position-index length)
    (ebrowse-view/find-position (nth ebrowse-position-index
				     ebrowse-position-stack))))


;;;###autoload
(defun ebrowse-back-in-position-stack (arg)
  "Move backward in the position stack.
Prefix arg ARG says how much."
  (interactive "p")
  (ebrowse-move-in-position-stack (max 1 arg)))


;;;###autoload
(defun ebrowse-forward-in-position-stack (arg)
  "Move forward in the position stack.
Prefix arg ARG says how much."
  (interactive "p")
  (ebrowse-move-in-position-stack (min -1 (- arg))))



;;; Electric position list

(defvar ebrowse-electric-position-mode-map ()
  "Keymap used in electric position stack window.")


(defvar ebrowse-electric-position-mode-hook nil
  "If non-nil, its value is called by `ebrowse-electric-position-mode'.")


(unless ebrowse-electric-position-mode-map
  (let ((map (make-keymap))
	(submap (make-keymap)))
    (setq ebrowse-electric-position-mode-map map)
    (fillarray (car (cdr map)) 'ebrowse-electric-position-undefined)
    (fillarray (car (cdr submap)) 'ebrowse-electric-position-undefined)
    (define-key map "\e" submap)
    (define-key map "\C-z" 'suspend-frame)
    (define-key map "\C-h" 'Helper-help)
    (define-key map "?" 'Helper-describe-bindings)
    (define-key map "\C-c" nil)
    (define-key map "\C-c\C-c" 'ebrowse-electric-position-quit)
    (define-key map "q" 'ebrowse-electric-position-quit)
    (define-key map " " 'ebrowse-electric-select-position)
    (define-key map "\C-l" 'recenter)
    (define-key map "\C-u" 'universal-argument)
    (define-key map "\C-p" 'previous-line)
    (define-key map "\C-n" 'next-line)
    (define-key map "p" 'previous-line)
    (define-key map "n" 'next-line)
    (define-key map "v" 'ebrowse-electric-view-position)
    (define-key map "\C-v" 'scroll-up)
    (define-key map "\ev" 'scroll-down)
    (define-key map "\e\C-v" 'scroll-other-window)
    (define-key map "\e>" 'end-of-buffer)
    (define-key map "\e<" 'beginning-of-buffer)
    (define-key map "\e>" 'end-of-buffer)))

(put 'ebrowse-electric-position-mode 'mode-class 'special)
(put 'ebrowse-electric-position-undefined 'suppress-keymap t)


(defun ebrowse-electric-position-mode ()
  "Mode for electric position buffers.
Runs the hook `ebrowse-electric-position-mode-hook'."
  (kill-all-local-variables)
  (use-local-map ebrowse-electric-position-mode-map)
  (setq mode-name "Electric Position Menu"
	mode-line-buffer-identification "Electric Position Menu")
  (when (memq 'mode-name mode-line-format)
    (setq mode-line-format (copy-sequence mode-line-format))
    (setcar (memq 'mode-name mode-line-format) "Positions"))
  (make-local-variable 'Helper-return-blurb)
  (setq Helper-return-blurb "return to buffer editing"
	truncate-lines t
	buffer-read-only t
	major-mode 'ebrowse-electric-position-mode)
  (run-mode-hooks 'ebrowse-electric-position-mode-hook))


(defun ebrowse-draw-position-buffer ()
  "Display positions in buffer *Positions*."
  (set-buffer (get-buffer-create "*Positions*"))
  (setq buffer-read-only nil)
  (erase-buffer)
  (insert "File           Point  Description\n"
	  "----           -----  -----------\n")
  (dolist (position ebrowse-position-stack)
    (insert (file-name-nondirectory (ebrowse-position-file-name position)))
    (indent-to 15)
    (insert (int-to-string (ebrowse-position-point position)))
    (indent-to 22)
    (insert (ebrowse-position-name position) "\n"))
  (setq buffer-read-only t))


;;;###autoload
(defun ebrowse-electric-position-menu ()
  "List positions in the position stack in an electric buffer."
  (interactive)
  (unless ebrowse-position-stack
    (error "No positions remembered"))
  (let (select buffer window)
    (save-window-excursion
      (save-window-excursion (ebrowse-draw-position-buffer))
      (setq window (Electric-pop-up-window "*Positions*")
	    buffer (window-buffer window))
      (shrink-window-if-larger-than-buffer window)
      (unwind-protect
	  (progn
	    (set-buffer buffer)
	    (ebrowse-electric-position-mode)
	    (setq select
		  (catch 'ebrowse-electric-select-position
		    (message "<<< Press Space to bury the list >>>")
		    (let ((first (progn (goto-char (point-min))
					(forward-line 2)
					(point)))
			  (last (progn (goto-char (point-max))
				       (forward-line -1)
				       (point)))
			  (goal-column 0))
		      (goto-char first)
		      (Electric-command-loop 'ebrowse-electric-select-position
					     nil t
					     'ebrowse-electric-position-looper
					     (cons first last))))))
	(set-buffer buffer)
	(bury-buffer buffer)
	(message nil)))
    (when select
      (set-buffer buffer)
      (ebrowse-electric-find-position select))
    (kill-buffer buffer)))


(defun ebrowse-electric-position-looper (state condition)
  "Prevent moving point on invalid lines.
Called from `Electric-command-loop'.  See there for the meaning
of STATE and CONDITION."
  (cond ((and condition
	      (not (memq (car condition) '(buffer-read-only
					   end-of-buffer
					   beginning-of-buffer))))
	 (signal (car condition) (cdr condition)))
	((< (point) (car state))
	 (goto-char (point-min))
	 (forward-line 2))
	((> (point) (cdr state))
	 (goto-char (point-max))
	 (forward-line -1)
	 (if (pos-visible-in-window-p (point-max))
	     (recenter -1)))))


(defun ebrowse-electric-position-undefined ()
  "Function called for undefined keys."
  (interactive)
  (message "Type C-h for help, ? for commands, q to quit, Space to execute")
  (sit-for 4))


(defun ebrowse-electric-position-quit ()
  "Leave the electric position list."
  (interactive)
  (throw 'ebrowse-electric-select-position nil))


(defun ebrowse-electric-select-position ()
  "Select a position from the list."
  (interactive)
  (throw 'ebrowse-electric-select-position (point)))


(defun ebrowse-electric-find-position (point &optional view)
  "View/find what is described by the line at POINT.
If VIEW is non-nil, view else find source files."
  (let ((index (- (count-lines (point-min) point) 2)))
    (ebrowse-view/find-position (nth index
				     ebrowse-position-stack) view)))


(defun ebrowse-electric-view-position ()
  "View the position described by the line point is in."
  (interactive)
  (ebrowse-electric-find-position (point) t))



;;; Saving trees to disk

(defun ebrowse-write-file-hook-fn ()
  "Write current buffer as a class tree.
Installed on `local-write-file-hooks'."
  (ebrowse-save-tree)
  t)


;;;###autoload
(defun ebrowse-save-tree ()
  "Save current tree in same file it was loaded from."
  (interactive)
  (ebrowse-save-tree-as (or buffer-file-name ebrowse--tags-file-name)))


;;;###autoload
(defun ebrowse-save-tree-as (&optional file-name)
  "Write the current tree data structure to a file.
Read the file name from the minibuffer if interactive.
Otherwise, FILE-NAME specifies the file to save the tree in."
  (interactive "FSave tree as: ")
  (let ((temp-buffer (get-buffer-create "*Tree Output"))
	(old-standard-output standard-output)
	(header (copy-ebrowse-hs ebrowse--header))
	(tree ebrowse--tree))
    (unwind-protect
	(save-excursion
	  (set-buffer (setq standard-output temp-buffer))
	  (erase-buffer)
	  (setf (ebrowse-hs-member-table header) nil)
	  (insert (prin1-to-string header) " ")
	  (mapc 'ebrowse-save-class tree)
	  (write-file file-name)
	  (message "Tree written to file `%s'" file-name))
      (kill-buffer temp-buffer)
      (set-buffer-modified-p nil)
      (ebrowse-update-tree-buffer-mode-line)
      (setq standard-output old-standard-output))))


(defun ebrowse-save-class (class)
  "Write single class CLASS to current buffer."
  (message "%s..." (ebrowse-cs-name (ebrowse-ts-class class)))
  (insert "[ebrowse-ts ")
  (prin1 (ebrowse-ts-class class))	;class name
  (insert "(")				;list of subclasses
  (mapc 'ebrowse-save-class (ebrowse-ts-subclasses class))
  (insert ")")
  (dolist (func ebrowse-member-list-accessors)
    (prin1 (funcall func class))
    (insert "\n"))
  (insert "()")				;base-classes slot
  (prin1 (ebrowse-ts-mark class))
  (insert "]\n"))



;;; Statistics

;;;###autoload
(defun ebrowse-statistics ()
  "Display statistics for a class tree."
  (interactive)
  (let ((tree-file (buffer-file-name))
	temp-buffer-setup-hook)
    (with-output-to-temp-buffer "*Tree Statistics*"
      (multiple-value-bind (classes member-functions member-variables
				    static-functions static-variables)
	  (ebrowse-gather-statistics)
	(set-buffer standard-output)
	(erase-buffer)
	(insert "STATISTICS FOR TREE " (or tree-file "unknown") ":\n\n")
	(ebrowse-print-statistics-line "Number of classes:" classes)
	(ebrowse-print-statistics-line "Number of member functions:"
				       member-functions)
	(ebrowse-print-statistics-line "Number of member variables:"
				       member-variables)
	(ebrowse-print-statistics-line "Number of static functions:"
				       static-functions)
	(ebrowse-print-statistics-line "Number of static variables:"
				       static-variables)))))


(defun ebrowse-print-statistics-line (title value)
  "Print a line in the statistics buffer.
TITLE is the title of the line, VALUE is a number to be printed
after that."
  (insert title)
  (indent-to 40)
  (insert (format "%d\n" value)))


(defun ebrowse-gather-statistics ()
  "Return statistics for a class tree.
The result is a list (NUMBER-OF-CLASSES NUMBER-OF-MEMBER-FUNCTIONS
NUMBER-OF-INSTANCE-VARIABLES NUMBER-OF-STATIC-FUNCTIONS
NUMBER-OF-STATIC-VARIABLES:"
  (let ((classes 0) (member-functions 0) (member-variables 0)
	(static-functions 0) (static-variables 0))
    (ebrowse-for-all-trees (tree ebrowse--tree-obarray)
      (incf classes)
      (incf member-functions (length (ebrowse-ts-member-functions tree)))
      (incf member-variables (length (ebrowse-ts-member-variables tree)))
      (incf static-functions (length (ebrowse-ts-static-functions tree)))
      (incf static-variables (length (ebrowse-ts-static-variables tree))))
    (list classes member-functions member-variables
	  static-functions static-variables)))



;;; Global key bindings

;;; The following can be used to bind key sequences starting with
;;; prefix `\C-c\C-m' to browse commands.

(defvar ebrowse-global-map nil
  "*Keymap for Ebrowse commands.")


(defvar ebrowse-global-prefix-key "\C-c\C-m"
  "Prefix key for Ebrowse commands.")


(defvar ebrowse-global-submap-4 nil
  "Keymap used for `ebrowse-global-prefix' followed by `4'.")


(defvar ebrowse-global-submap-5 nil
  "Keymap used for `ebrowse-global-prefix' followed by `5'.")


(unless ebrowse-global-map
  (setq ebrowse-global-map (make-sparse-keymap))
  (setq ebrowse-global-submap-4 (make-sparse-keymap))
  (setq ebrowse-global-submap-5 (make-sparse-keymap))
  (define-key ebrowse-global-map "a" 'ebrowse-tags-apropos)
  (define-key ebrowse-global-map "b" 'ebrowse-pop-to-browser-buffer)
  (define-key ebrowse-global-map "-" 'ebrowse-back-in-position-stack)
  (define-key ebrowse-global-map "+" 'ebrowse-forward-in-position-stack)
  (define-key ebrowse-global-map "l" 'ebrowse-tags-list-members-in-file)
  (define-key ebrowse-global-map "m" 'ebrowse-tags-display-member-buffer)
  (define-key ebrowse-global-map "n" 'ebrowse-tags-next-file)
  (define-key ebrowse-global-map "p" 'ebrowse-electric-position-menu)
  (define-key ebrowse-global-map "s" 'ebrowse-tags-search)
  (define-key ebrowse-global-map "u" 'ebrowse-tags-search-member-use)
  (define-key ebrowse-global-map "v" 'ebrowse-tags-view-definition)
  (define-key ebrowse-global-map "V" 'ebrowse-tags-view-declaration)
  (define-key ebrowse-global-map "%" 'ebrowse-tags-query-replace)
  (define-key ebrowse-global-map "." 'ebrowse-tags-find-definition)
  (define-key ebrowse-global-map "f" 'ebrowse-tags-find-definition)
  (define-key ebrowse-global-map "F" 'ebrowse-tags-find-declaration)
  (define-key ebrowse-global-map "," 'ebrowse-tags-loop-continue)
  (define-key ebrowse-global-map " " 'ebrowse-electric-buffer-list)
  (define-key ebrowse-global-map "\t" 'ebrowse-tags-complete-symbol)
  (define-key ebrowse-global-map "4" ebrowse-global-submap-4)
  (define-key ebrowse-global-submap-4 "." 'ebrowse-tags-find-definition-other-window)
  (define-key ebrowse-global-submap-4 "f" 'ebrowse-tags-find-definition-other-window)
  (define-key ebrowse-global-submap-4 "v" 'ebrowse-tags-find-declaration-other-window)
  (define-key ebrowse-global-submap-4 "F" 'ebrowse-tags-view-definition-other-window)
  (define-key ebrowse-global-submap-4 "V" 'ebrowse-tags-view-declaration-other-window)
  (define-key ebrowse-global-map "5" ebrowse-global-submap-5)
  (define-key ebrowse-global-submap-5 "." 'ebrowse-tags-find-definition-other-frame)
  (define-key ebrowse-global-submap-5 "f" 'ebrowse-tags-find-definition-other-frame)
  (define-key ebrowse-global-submap-5 "v" 'ebrowse-tags-find-declaration-other-frame)
  (define-key ebrowse-global-submap-5 "F" 'ebrowse-tags-view-definition-other-frame)
  (define-key ebrowse-global-submap-5 "V" 'ebrowse-tags-view-declaration-other-frame)
  (define-key global-map ebrowse-global-prefix-key ebrowse-global-map))



;;; Electric C++ browser buffer menu

;;; Electric buffer menu customization to display only some buffers
;;; (in this case Tree buffers).  There is only one problem with this:
;;; If the very first character typed in the buffer menu is a space,
;;; this will select the buffer from which the buffer menu was
;;; invoked.  But this buffer is not displayed in the buffer list if
;;; it isn't a tree buffer.  I therefore let the buffer menu command
;;; loop read the command `p' via `unread-command-char'.  This command
;;; has no effect since we are on the first line of the buffer.

(defvar electric-buffer-menu-mode-hook nil)


(defun ebrowse-hack-electric-buffer-menu ()
  "Hack the electric buffer menu to display browser buffers."
  (let (non-empty)
    (unwind-protect
	(save-excursion
	  (setq buffer-read-only nil)
	  (goto-char 1)
	  (forward-line 2)
	  (while (not (eobp))
	    (let ((b (Buffer-menu-buffer nil)))
	      (if (or (ebrowse-buffer-p b)
		      (string= (buffer-name b) "*Apropos Members*"))
		  (progn (forward-line 1)
			 (setq non-empty t))
		(delete-region (point)
			       (save-excursion (end-of-line)
					       (min (point-max)
						    (1+ (point)))))))))
      (unless non-empty
	(error "No tree buffers"))
      (setf unread-command-events (listify-key-sequence "p"))
      (shrink-window-if-larger-than-buffer (selected-window))
      (setq buffer-read-only t))))


(defun ebrowse-select-1st-to-9nth ()
  "Select the nth entry in the list by the keys 1..9."
  (interactive)
  (let* ((maxlin (count-lines (point-min) (point-max)))
	 (n (min maxlin (+ 2 (string-to-number (this-command-keys))))))
    (goto-line n)
    (throw 'electric-buffer-menu-select (point))))


(defun ebrowse-install-1-to-9-keys ()
  "Define keys 1..9 to select the 1st to 9nth entry in the list."
  (dotimes (i 9)
    (define-key (current-local-map) (char-to-string (+ i ?1))
      'ebrowse-select-1st-to-9nth)))


(defun ebrowse-electric-buffer-list ()
  "Display an electric list of Ebrowse buffers."
  (interactive)
  (unwind-protect
      (progn
	(add-hook 'electric-buffer-menu-mode-hook
		  'ebrowse-hack-electric-buffer-menu)
	(add-hook 'electric-buffer-menu-mode-hook
		  'ebrowse-install-1-to-9-keys)
	(call-interactively 'electric-buffer-list))
    (remove-hook 'electric-buffer-menu-mode-hook
		 'ebrowse-hack-electric-buffer-menu)))


;;; Mouse support

(defun ebrowse-mouse-find-member (event)
  "Find the member clicked on in another frame.
EVENT is a mouse button event."
  (interactive "e")
  (mouse-set-point event)
  (let (start name)
    (save-excursion
      (skip-chars-backward "a-zA-Z0-9_")
      (setq start (point))
      (skip-chars-forward "a-zA-Z0-9_")
      (setq name (buffer-substring start (point))))
    (ebrowse-tags-view/find-member-decl/defn
     5 :view nil :definition t :member-name name)))


(defun ebrowse-popup-menu (menu event)
  "Pop up MENU and perform an action if something was selected.
EVENT is the mouse event."
  (save-selected-window
    (select-window (posn-window (event-start event)))
    (let ((selection (x-popup-menu event menu)) binding)
      (while selection
	(setq binding (lookup-key (or binding menu) (vector (car selection)))
	      selection (cdr selection)))
      (when binding
	(call-interactively binding)))))


(easy-menu-define
 ebrowse-tree-buffer-class-object-menu ebrowse-tree-mode-map
 "Object menu for classes in the tree buffer"
 '("Class"
   ["Functions" ebrowse-tree-command:show-member-functions
    :help "Display a list of member functions"
    :active t]
   ["Variables" ebrowse-tree-command:show-member-variables
    :help "Display a list of member variables"
    :active t]
   ["Static Functions" ebrowse-tree-command:show-static-member-functions
    :help "Display a list of static member functions"
    :active t]
   ["Static Variables" ebrowse-tree-command:show-static-member-variables
    :help "Display a list of static member variables"
    :active t]
   ["Friends/ Defines" ebrowse-tree-command:show-friends
    :help "Display a list of friends of a class"
    :active t]
   ["Types" ebrowse-tree-command:show-types
    :help "Display a list of types defined in a class"
    :active t]
   "-----------------"
   ["View" ebrowse-view-class-declaration
    :help "View class declaration"
    :active (eq (get-text-property (point) 'ebrowse-what) 'class-name)]
   ["Find" ebrowse-find-class-declaration
    :help "Find class declaration in file"
    :active (eq (get-text-property (point) 'ebrowse-what) 'class-name)]
   "-----------------"
   ["Mark" ebrowse-toggle-mark-at-point
    :help "Mark class point is on"
    :active (eq (get-text-property (point) 'ebrowse-what) 'class-name)]
   "-----------------"
   ["Collapse" ebrowse-collapse-branch
    :help "Collapse subtree under class point is on"
    :active (eq (get-text-property (point) 'ebrowse-what) 'class-name)]
   ["Expand" ebrowse-expand-branch
    :help "Expand subtree under class point is on"
    :active (eq (get-text-property (point) 'ebrowse-what) 'class-name)]))


(easy-menu-define
 ebrowse-tree-buffer-object-menu ebrowse-tree-mode-map
 "Object menu for tree buffers"
 '("Ebrowse"
   ["Filename Display" ebrowse-toggle-file-name-display
    :help "Toggle display of source files names"
    :style toggle
    :selected ebrowse--show-file-names-flag
    :active t]
   ["Tree Indentation" ebrowse-set-tree-indentation
    :help "Set the tree's indentation"
    :active t]
   ["Unmark All Classes" ebrowse-mark-all-classes
    :help "Unmark all classes in the class tree"
    :active t]
   ["Expand All" ebrowse-expand-all
    :help "Expand all subtrees in the class tree"
    :active t]
   ["Statistics" ebrowse-statistics
    :help "Show a buffer with class hierarchy statistics"
    :active t]
   ["Find Class" ebrowse-read-class-name-and-go
    :help "Find a class in the tree"
    :active t]
   ["Member Buffer" ebrowse-pop/switch-to-member-buffer-for-same-tree
    :help "Show a member buffer for this class tree"
    :active t]))


(defun ebrowse-mouse-3-in-tree-buffer (event)
  "Perform mouse actions in tree buffers.
EVENT is the mouse event."
  (interactive "e")
  (mouse-set-point event)
  (let* ((where (posn-point (event-start event)))
	 (property (get-text-property where 'ebrowse-what)))
    (case (event-click-count event)
      (1
       (case property
	 (class-name
	  (ebrowse-popup-menu ebrowse-tree-buffer-class-object-menu event))
	 (t
	  (ebrowse-popup-menu ebrowse-tree-buffer-object-menu event)))))))


(defun ebrowse-mouse-2-in-tree-buffer (event)
  "Perform mouse actions in tree buffers.
EVENT is the mouse event."
  (interactive "e")
  (mouse-set-point event)
  (let* ((where (posn-point (event-start event)))
	 (property (get-text-property where 'ebrowse-what)))
    (case (event-click-count event)
      (1 (case property
	   (class-name
	    (ebrowse-tree-command:show-member-functions)))))))


(defun ebrowse-mouse-1-in-tree-buffer (event)
  "Perform mouse actions in tree buffers.
EVENT is the mouse event."
  (interactive "e")
  (mouse-set-point event)
  (let* ((where (posn-point (event-start event)))
	 (property (get-text-property where 'ebrowse-what)))
    (case (event-click-count event)
      (2 (case property
	   (class-name
	    (let ((collapsed (save-excursion (skip-chars-forward "^\r\n")
					     (looking-at "\r"))))
	      (ebrowse-collapse-fn (not collapsed))))
	   (mark
	    (ebrowse-toggle-mark-at-point 1)))))))



(provide 'ebrowse)

;;; Local variables:
;;; eval:(put 'ebrowse-output 'lisp-indent-hook 0)
;;; eval:(put 'ebrowse-ignoring-completion-case 'lisp-indent-hook 0)
;;; eval:(put 'ebrowse-save-selective 'lisp-indent-hook 0)
;;; eval:(put 'ebrowse-for-all-trees 'lisp-indent-hook 1)
;;; End:

;; arch-tag: 4fa3c8bf-1771-479b-bcd7-b029c7c9677b
;;; ebrowse.el ends here