Source

dired / dired.el

Full commit
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
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
;; -*- Emacs-Lisp -*-
;; DIRED commands for Emacs.
;; Copyright (C) 1985, 1986, 1991 Free Software Foundation, Inc.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; File:          dired.el
;; RCS:           
;; Dired Version: #Revision: 7.10 $
;; Description:   The DIRectory EDitor is for manipulating, and running
;;                commands on files in a directory.
;; Authors:       FSF,
;;                Sebastian Kremer <sk@thp.uni-koeln.de>,
;;                Sandy Rutherford <sandy@ibm550.sissa.it>
;;                Cast of thousands...
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; This program 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 1, or (at your option)
;; any later version.

;; This program 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; see the file COPYING.  If not, write to
;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

;; Rewritten in 1990/1991 to add tree features, file marking and
;; sorting by Sebastian Kremer <sk@thp.uni-koeln.de>.
;; 7-1993: Added special features for efs interaction and upgraded to Emacs 19.
;;         Sandy Rutherford <sandy@ibm550.sissa.it>

;;; Dired Version

(defconst dired-version (substring "#Revision: 7.10 $" 11 -2)
  "The revision number of Tree Dired (as a string).

Don't forget to mention this when reporting bugs to:
   
   efs-bugs@hplb.hpl.hp.com")

;; Global key bindings:
;; --------------------
;;
;; By convention, dired uses the following global key-bindings.
;; These may or may not already be set up in your local emacs. If not
;; then you will need to add them to your .emacs file, or the system
;; default.el file. We don't set them automatically here, as users may
;; have individual preferences.
;;
;; (define-key ctl-x-map "d" 'dired)
;; (define-key ctl-x-4-map "d" 'dired-other-window)
;; (define-key ctl-x-map "\C-j" 'dired-jump-back)
;; (define-key ctl-x-4-map "\C-j" 'dired-jump-back-other-window)
;; 
;; For V19 emacs only. (Make sure that the ctl-x-5-map exists.)
;; (define-key ctl-x-5-map "d" 'dired-other-frame)
;; (define-key Ctl-x-5-map "\C-j" 'dired-jump-back-other-frame)


;;; Grok the current emacs version
;;
;; Hopefully these two variables provide us with enough version sensitivity.

;; Make sure that we have a frame-width function
(or (fboundp 'frame-width) (fset 'frame-width 'screen-width))

;;; Requirements and provisions

(provide 'dired)
(require 'backquote) ; For macros.
(require 'custom)

;; Compatibility requirements for the file-name-handler-alist.
;; Testing against the string `Lucid' breaks InfoDock.  How many years has
;; it been since Lucid went away?
(let ((lucid-p (string-match "XEmacs" emacs-version))
      ver subver)
  (or (string-match "^\\([0-9]+\\)\\.\\([0-9]+\\)" emacs-version)
      (error "dired does not work with emacs version %s" emacs-version))
  (setq ver (string-to-int (substring emacs-version (match-beginning 1)
				      (match-end 1)))
	subver (string-to-int (substring emacs-version (match-beginning 2)
					 (match-end 2))))
  (cond
   ((= ver 18)
    (require 'emacs-19)
    (require 'fn-handler))
   ((and (= ver 19) (if lucid-p (< subver 10) (< subver 23)))
    (require 'fn-handler))
   ((< ver 18)
    (error "dired does not work with emacs version %s" emacs-version))))

;; We duplicate default-dir stuff to avoid its overwrites unless
;; they are explicitly requested.

(defvar default-directory-function nil
  "A function to call to compute the default-directory for the current buffer.
If this is nil, the function default-directory will return the value of the
variable default-directory.
Buffer local.")
(make-variable-buffer-local 'default-directory-function)

;;;###autoload
(defun default-directory ()
  " Returns the default-directory for the current buffer.
Will use the variable default-directory-function if it non-nil."
  (if default-directory-function
      (funcall default-directory-function)
    (if (string-match "XEmacs" emacs-version)
	(abbreviate-file-name default-directory t)
      (abbreviate-file-name default-directory))))

;;;;----------------------------------------------------------------
;;;; Customizable variables
;;;;----------------------------------------------------------------
;;

(defgroup dired nil
  "Directory editing."
  :group 'environment)

;; The funny comments are for autoload.el, to automagically update
;; loaddefs.

;;; Variables for compressing files.

(defgroup dired-programs nil
  "External programs used by Dired."
  :group 'dired)

;;;###autoload
(defcustom dired-compression-method 'gzip
  "*Type of compression program to use.
Give as a symbol.
Currently-recognized methods are: gzip pack compact compress.
To change this variable use \\[dired-do-compress] with a zero prefix."
  :type '(choice (const gzip)
		 (const pack)
		 (const pack)
		 (const compact)
		 (const compress))
  :group 'dired-programs)

;;;###autoload
(defvar dired-compression-method-alist
  '((gzip     ".gz" ("gzip")          ("gzip" "-d") "-f")
    ;; Put compress before pack, so that it wins out if we are using
    ;; efs to work on a case insensitive OS. The -f flag does
    ;; two things in compress. No harm in giving it twice.
    (compress ".Z"  ("compress" "-f") ("compress" "-d") "-f")
    ;; pack support may not work well.  pack is too chatty and there is no way
    ;; to force overwrites.
    (pack     ".z"  ("pack" "-f")     ("unpack"))
    (compact  ".C"  ("compact")       ("uncompact"))
    (bzip2    ".bz2" ("bzip2")        ("bunzip2") "-f"))
  
  "*Association list of compression method descriptions.
 Each element of the table should be a list of the form
 
     \(compress-type extension (compress-args) (decompress-args) force-flag\)
 
 where 
   `compress-type' is a unique symbol in the alist to which
      `dired-compression-method' can be set;
   `extension' is the file extension (as a string) used by files compressed
      by this method;
   `compress-args' is a list of the path of the compression program and
      flags to pass as separate arguments;
   `decompress-args' is a list of the path of the decompression
      program and flags to pass as separate arguments.
   `force-flag' is the switch to pass to the command to force overwriting
      of existing files.
 
 For example:
 
   \(setq dired-compression-method-alist
         \(cons '\(frobnicate \".frob\" \(\"frob\"\) \(\"frob\" \"-d\"\) \"-f\"\)
               dired-compression-method-alist\)\)
   => \(\(frobnicate \".frob\" \(\"frob\"\) \(\"frob\" \"-d\"\)\) 
       \(gzip \".gz\" \(\"gzip\"\) \(\"gunzip\"\)\)
       ...\)
 
 See also: dired-compression-method <V>")

;;; Variables for the ls program.

;;;###autoload
(defcustom dired-ls-program "ls"
  "*Absolute or relative name of the ls program used by dired."
  :type 'string
  :group 'dired-programs)

;;;###autoload
(defcustom dired-listing-switches "-al"
  "*Switches passed to ls for dired. MUST contain the `l' option.
Can contain even `F', `b', `i' and `s'."
  :type '(choice string
		 (repeat string))
  :group 'dired-programs)

(defcustom dired-ls-F-marks-symlinks
  (memq system-type '(aix-v3 hpux silicon-graphics-unix))
  ;; Both SunOS and Ultrix have system-type berkeley-unix. But
  ;; SunOS doesn't mark symlinks, but Ultrix does. Therefore,
  ;; can't grok this case.
  "*Informs dired about how ls -lF marks symbolic links.
Set this to t if `dired-ls-program' with -lF marks the name of the symbolic
link itself with a trailing @.

For example: If foo is a link pointing to bar, and \"ls -F bar\" gives 

     ...   bar -> foo

set this variable to nil. If it gives
    
      ...   bar@ -> foo

set this variable to t.

Dired checks if there is really a @ appended.  Thus, if you have a
marking ls program on one host and a non-marking on another host, and
don't care about symbolic links which really end in a @, you can
always set this variable to t.

If you use efs, it will make this variable buffer-local, and control
it according to its assessment of how the remote host marks symbolic
links."
  :type 'boolean
  :group 'dired-programs)

(defcustom dired-show-ls-switches nil
  "*If non-nil dired will show the dired ls switches on the modeline.
If nil, it will indicate how the files are sorted by either \"by name\" or
\"by date\". If it is unable to recognize the sorting defined by the switches,
then the switches will be shown explicitly on the modeline, regardless of the
setting of this variable."
  :type 'boolean
  :group 'dired-programs)

;;; Variables for other unix utility programs.

;; For most program names, don't use absolute paths so that dired
;; uses the user's value of the environment variable PATH. chown is
;; an exception as it is not always in the PATH.

;;;###autoload
(defcustom dired-chown-program
  (if (memq system-type '(hpux dgux usg-unix-v linux)) "chown" "/etc/chown")
  "*Name of chown command (usually `chown' or `/etc/chown')."
  :type 'string
  :group 'dired-programs)

;;;###autoload
(defcustom dired-gnutar-program nil
  "*If non-nil, name of the GNU tar executable (e.g. \"tar\" or \"gnutar\").
GNU tar's `z' switch is used for compressed tar files.
If you don't have GNU tar, set this to nil: a pipe using `zcat' is then used."
  :type '(choice string
		 (const nil))
  :group 'dired-programs)

;;;###autoload
(defcustom dired-unshar-program nil
  "*Set to the name of the unshar program, if you have it."
  :type '(choice string
		 (const nil))
  :group 'dired-programs)

;;; Markers

(defgroup dired-markers nil
  "Dealing with markers."
  :group 'dired)

(defcustom dired-keep-marker-rename t
  ;; Use t as default so that moved files `take their markers with them'
  "*Controls marking of renamed files.
If t, files keep their previous marks when they are renamed.
If a character, renamed files (whether previously marked or not)
are afterward marked with that character."
  :type '(choice (const t)
		 character)
  :group 'dired-markers)

(defcustom dired-keep-marker-compress t
  "*Controls marking of compressed or uncompressed files.
If t, files keep their previous marks when they are compressed.
If a character, compressed or uncompressed files (whether previously
marked or not) are afterward marked with that character."
  :type '(choice (const t)
		 character)
  :group 'dired-markers)

(defcustom dired-keep-marker-uucode ?U
  "*Controls marking of uuencoded or uudecoded files.
If t, files keep their previous marks when they are uuencoded.
If a character, uuencoded or uudecoded files (whether previously
marked or not) are afterward marked with that character."
  :type '(choice (const t)
		 character)
  :group 'dired-markers)

(defcustom dired-keep-marker-copy ?C
  "*Controls marking of copied files.
If t, copied files are marked if and as the corresponding original files were.
If a character, copied files are unconditionally marked with that character."
  :type '(choice (const t)
		 character)
  :group 'dired-markers)

(defcustom dired-keep-marker-hardlink ?H
  "*Controls marking of newly made hard links.
If t, they are marked if and as the files linked to were marked.
If a character, new links are unconditionally marked with that character."
  :type '(choice (const t)
		 character)
  :group 'dired-markers)


(defcustom dired-keep-marker-symlink ?S
  "*Controls marking of newly made symbolic links.
If t, they are marked if and as the files linked to were marked.
If a character, new links are unconditionally marked with that character."
  :type '(choice (const t)
		 character)
  :group 'dired-markers)

(defcustom dired-keep-marker-kill ?K
  "*When killed file lines are redisplayed, they will have this marker.
Setting this to nil means that they will not have any marker."
  :type '(choice (const nil)
		 character)
  :group 'dired-markers)

(defcustom dired-failed-marker-shell ?!
  "*If non-nil, a character with which to mark files of failed shell commands.
Applies to the command `dired-do-shell-command'.  Files for which the shell
command has a nonzero exit status will be marked with this character"
  :type '(choice (const nil)
		 character)
  :group 'dired-markers)

;;; Behavioral Variables

(defgroup dired-behavior nil
  "Dired behavior."
  :group 'dired)

;;;###autoload
(defcustom dired-local-variables-file ".dired"
  "*If non-nil, filename for local variables for Dired.
If Dired finds a file with that name in the current directory, it will
temporarily insert it into the dired buffer and run `hack-local-variables'.

Type \\[info] and `g' `(emacs)File Variables' `RET' for more info on
local variables."
  :type 'string
  :group 'dired-behavior)

;; Usually defined in files.el. Define here anyway, to be safe.
;;;###autoload
(defcustom dired-kept-versions 2
  "*When cleaning directory, number of versions to keep."
  :type 'integer
  :group 'dired-behavior)

;;;###autoload
(defcustom dired-find-subdir nil
  "*Determines whether dired tries to lookup a subdir in existing buffers.
If non-nil, dired does not make a new buffer for a directory if it can be
found (perhaps as subdir) in some existing dired buffer. If there are several
dired buffers for a directory, then the most recently used one is chosen.

Dired avoids switching to the current buffer, so that if you have
a normal and a wildcard buffer for the same directory, C-x d RET will
toggle between those two."
  :type 'boolean
  :group 'dired-behavior)

;;;###autoload
(defcustom dired-use-file-transformers t
  "*Determines whether dired uses file transformers.
If non-nil `dired-do-shell-command' will apply file transformers to file names.
See \\[describe-function] for dired-do-shell-command for more information."
  :type 'boolean
  :group 'dired-behavior)

;;;###autoload
(defcustom dired-dwim-target nil
  "*If non-nil, dired tries to guess a default target directory.
This means that if there is a dired buffer displayed in the next window,
use its current subdir, instead of the current subdir of this dired buffer.
The target is put in the prompt for file copy, rename, etc."
  :type 'boolean
  :group 'dired-behavior)

;;;###autoload
(defcustom dired-copy-preserve-time t
  "*If non-nil, Dired preserves the last-modified time in a file copy.
\(This works on only some systems.)\\<dired-mode-map>
Use `\\[dired-do-copy]' with a zero prefix argument to toggle its value."
  :type 'boolean
  :group 'dired-behavior)

;;;###autoload
(defcustom dired-no-confirm nil
  "*If non-nil, a list of symbols for commands dired should not confirm.
It can be a sublist of

  '(byte-compile chgrp chmod chown compress copy delete hardlink load
    move print shell symlink uncompress recursive-delete kill-file-buffer
    kill-dired-buffer patch create-top-dir revert-subdirs)

The meanings of most of the symbols are obvious.  A few exceptions:

    'compress applies to compression or decompression by any of the 
     compression program in `dired-compression-method-alist'.

    'kill-dired-buffer applies to offering to kill dired buffers for
     directories which have been deleted.

    'kill-file-buffer applies to offering to kill buffers visiting files
     which have been deleted.

    'recursive-delete applies to recursively deleting non-empty
     directories, and all of their contents.

    'create-top-dir applies to `dired-up-directory' creating a new top level
     directory for the dired buffer.

    'revert-subdirs applies to re-reading subdirectories which have 
     been modified on disk.

Note that this list also applies to remote files accessed with efs
or ange-ftp."
  :type '(set (const byte-compile) (const chgrp) (const chmod)
	      (const chown) (const compress) (const copy)
	      (const delete) (const hardlink) (const load)
	      (const move) (const print) (const shell)
	      (const symlink) (const uncompress)
	      (const recursive-delete) (const kill-file-buffer)
	      (const kill-dired-buffer) (const patch)
	      (const create-top-dir) (const revert-subdirs))
:group 'dired-behavior)
		      
;;;###autoload
(defcustom dired-backup-if-overwrite nil
  "*Non-nil if Dired should ask about making backups before overwriting files.
Special value 'always suppresses confirmation."
  :type 'boolean
  :group 'dired-behavior)

;;;###autoload
(defcustom dired-omit-files nil
  "*If non-nil un-interesting files will be omitted from this dired buffer.
Use \\[dired-omit-toggle] to see these files. (buffer local)"
  :type 'boolean
  :group 'dired-behavior)
(make-variable-buffer-local 'dired-omit-files)

;;;###autoload
(defcustom dired-mail-reader 'vm
  "*Mail reader used by dired for dired-read-mail \(\\[dired-read-mail]\).
The symbols 'rmail and 'vm are the only two allowed values."
  :type '(choice (const rmail)
		 (const vm))
  :group 'dired-behavior)

(defcustom dired-verify-modtimes t
  "*If non-nil dired will revert dired buffers for modified subdirectories.
See also dired-no-confirm <V>."
  :type 'boolean
  :group 'dired-behavior)

;;;###autoload
(defcustom dired-refresh-automatically t
  "*If non-nil, refresh dired buffers automatically after file operations."
  :type 'boolean
  :group 'dired-behavior)

;;; File name regular expressions and extensions.

(defgroup dired-file-names nil
  "How Dired deals with files with certain names."
  :group 'dired)

(defcustom dired-trivial-filenames "\\`\\.\\.?\\'\\|\\`#"
  "*Regexp of files to skip when finding first file of a directory listing.
A value of nil means move to the subdir line.
A value of t means move to first file."
  :type 'string
  :group 'dired-file-names)

(defcustom dired-cleanup-alist
  (list
   '("tex" ".toc" ".log" ".aux" ".dvi")
   '("latex" ".toc" ".log" ".aux" ".idx" ".lof" ".lot" ".glo" ".dvi")
   '("bibtex" ".blg" ".bbl")
   '("texinfo" ".cp" ".cps" ".fn" ".fns" ".ky" ".kys" ".pg" ".pgs"
     ".tp" ".tps" ".vr" ".vrs")
   '("patch" ".rej" ".orig")
   '("backups" "~")
   (cons "completion-ignored-extensions" completion-ignored-extensions))
  "*Alist of extensions for temporary files created by various programs.
Used by `dired-cleanup'."
  :type '(list (cons string (list string)))
  :group 'dired-file-names)

(defcustom dired-omit-extensions
  (let ((alist dired-cleanup-alist)
	x result)
    (while alist
      (setq x (cdr (car alist))
	    alist (cdr alist))
      (while x
	(or (member (car x) result)
	    (setq result (cons (car x) result)))
	(setq x (cdr x))))
    result)
  "*List of extensions for file names that will be omitted (buffer-local).
This only has effect when the subdirectory is in omission mode.
To make omission mode the default, set `dired-omit-files' to t.
See also `dired-omit-regexps'."
  :type '(list string)
  :group 'dired-file-names)
(make-variable-buffer-local 'dired-omit-extensions)

(defcustom dired-omit-regexps '("\\`#" "\\`\\.")
  "*File names matching these regexp may be omitted (buffer-local).
This only has effect when the subdirectory is in omission mode.
To make omission mode the default, set `dired-omit-files' to t.
This only has effect when `dired-omit-files' is t.
See also `dired-omit-extensions'."
  :type '(list string)
  :group 'dired-file-names)
(make-variable-buffer-local 'dired-omit-regexps)

(defcustom dired-filename-re-ext "\\..+\\'"   ; start from the first dot. last dot?
  "*Defines what is the extension of a file name.
\(match-beginning 0\) for this regexp in the file name without directory will
be taken to be the start of the extension."
  :type 'string
  :group 'dired-file-names)

;;; Hook variables

(defgroup dired-hooks nil
  "Dired hooks."
  :group 'dired)

(defcustom dired-load-hook nil
  "Run after loading dired.
You can customize key bindings or load extensions with this."
  :type 'hook
  :group 'dired-hooks)

(defcustom dired-grep-load-hook nil
  "Run after loading dired-grep."
  :type 'hook
  :group 'dired-hooks)

(defcustom dired-mode-hook nil
  "Run at the very end of dired-mode."
  :type 'hook
  :group 'dired-hooks)

(defcustom dired-before-readin-hook nil
  "Hook run before a dired buffer is newly read in, created,or reverted."
  :type 'hook
  :group 'dired-hooks)

(defcustom dired-after-readin-hook nil
  "Hook run after each listing of a file or directory.
The buffer is narrowed to the new listing."
  :type 'hook
  :group 'dired-hooks)

(defcustom dired-setup-keys-hook nil
  "Hook run when dired sets up its keymap.
This happens the first time that `dired-mode' is called, and runs after
`dired-mode-hook'.  This hook can be used to make alterations to the
dired keymap."
  :type 'hook
  :group 'dired-hooks)

;;; Internal variables
;;
;;  If you set these, know what you are doing.

;;; Marker chars.

(defvar dired-marker-char ?*            ; the answer is 42
					; life the universe and everything
  ;; so that you can write things like
  ;; (let ((dired-marker-char ?X))
  ;;    ;; great code using X markers ...
  ;;    )
  ;; For example, commands operating on two sets of files, A and B.
  ;; Or marking files with digits 0-9.  This could implicate
  ;; concentric sets or an order for the marked files.
  ;; The code depends on dynamic scoping on the marker char.
  "In dired, character used to mark files for later commands.")
(make-variable-buffer-local 'dired-marker-char)

(defconst dired-default-marker dired-marker-char)
;; Stores the default value of dired-marker-char when dynamic markers
;; are being used.

(defvar dired-del-marker ?D
  "Character used to flag files for deletion.")

;; \017=^O for Omit - other packages can chose other control characters.
(defvar dired-omit-marker-char ?\017)
;; Marker used for omitted files.  Shouldn't be used by anything else.

(defvar dired-kill-marker-char ?\C-k)
;; Marker used by dired-do-kill.  Shouldn't be used by anything else.

;;; State variables

(defvar dired-mode-line-modified "-%s%s%s-"
  "*Format string to show the modification status of the buffer.")

(defvar dired-del-flags-number 0)
(make-variable-buffer-local 'dired-del-flags-number)
(defvar dired-marks-number 0)
(make-variable-buffer-local 'dired-marks-number)
(defvar dired-other-marks-number 0)
(make-variable-buffer-local 'dired-other-marks-number)

(defvar dired-marked-files nil
  "List of filenames from last `dired-copy-filename-as-kill' call.")

(defvar dired-directory nil
  "The directory name or shell wildcard that was used as argument to `ls'.
Local to each dired buffer.  May be a list, in which case the car is the
directory name and the cdr is the actual files to list.")
(make-variable-buffer-local 'dired-directory)

(defvar dired-internal-switches nil
  "The actual (buffer-local) value of `dired-listing-switches'.
The switches are represented as a list of characters.")
(make-variable-buffer-local 'dired-internal-switches)

(defvar dired-subdir-alist nil
  "Association list of subdirectories and their buffer positions.
Each subdirectory has an element: (DIRNAME . STARTMARKER).
The order of elements is the reverse of the order in the buffer.")
(make-variable-buffer-local 'dired-subdir-alist)

(defvar dired-curr-subdir-min 0)
;; Cache for modeline tracking of the cursor
(make-variable-buffer-local 'dired-curr-subdir-min)

(defvar dired-curr-subdir-max 0)
;; Cache for modeline tracking of the cursor
(make-variable-buffer-local 'dired-curr-subdir-max)

(defvar dired-subdir-omit nil)
;; Controls whether the modeline shows Omit.
(make-variable-buffer-local 'dired-subdir-omit)

(defvar dired-in-query nil)
;; let-bound to t when dired is in the process of querying the user.
;; This is to keep asynch messaging from clobbering the query prompt.

(defvar dired-overwrite-confirmed nil)
;; Fluid variable used to remember if a bunch of overwrites have been
;; confirmed.

(defvar dired-overwrite-backup-query nil)
;; Fluid var used to remember if backups have been requested for overwrites.

(defvar dired-file-creator-query nil)
;; Fluid var to remember responses to file-creator queries.

(defvar dired-omit-silent nil)
;; This is sometimes let-bound to t if messages would be annoying,
;; e.g., in dired-awrh.el. Binding to 0, only suppresses
;; \"(Nothing to omit)\" message.

(defvar dired-buffers nil
  ;; Enlarged by dired-advertise
  ;; Queried by function dired-buffers-for-dir. When this detects a
  ;; killed buffer, it is removed from this list.
  "Alist of directories and their associated dired buffers.")

(defvar dired-sort-mode nil
  "Whether Dired sorts by name, date, etc. 
\(buffer-local\)")
;; This is nil outside dired buffers so it can be used in the modeline
(make-variable-buffer-local 'dired-sort-mode)

(defvar dired-marker-stack nil
  "List of previously used dired marker characters.")
(make-variable-buffer-local 'dired-marker-stack)

(defvar dired-marker-stack-pointer 0)
;; Points to the current marker in the stack
(make-variable-buffer-local 'dired-marker-stack-pointer)

(defvar dired-marker-stack-cursor ?\  ; space
  "Character to use as a cursor in the dired marker stack.")

(defconst dired-marker-string ""
  "String version of `dired-marker-stack'.")
(make-variable-buffer-local 'dired-marker-string)

(defvar dired-modeline-tracking-cmds nil)
;; List of commands after which the modeline gets updated.

;;; Config. variables not usually considered fair game for the user.

(defvar dired-deletion-confirmer 'yes-or-no-p) ; or y-or-n-p?

(defvar dired-log-buffer "*Dired log*")
;; Name of buffer used to log dired messages and errors.

;;; Assoc. lists

;; For pop ups and user input for file marking
(defvar dired-query-alist
  '((?\y . y) (?\040 . y)		; `y' or SPC means accept once
    (?n . n) (?\177 . n)		; `n' or DEL skips once
    (?! . yes)				; `!' accepts rest
    (?q. no) (?\e . no)			; `q' or ESC skips rest
    ;; None of these keys quit - use C-g for that.
    ))

(defvar dired-sort-type-alist
  ;; alist of sort flags, and the sort type, as a symbol.
  ;; Don't put ?r in here.  It's handled separately.
  '((?t . date) (?S . size) (?U . unsort) (?X . ext)))

;;; Internal regexps for examining ls listings.
;;
;; Many of these regexps must be tested at beginning-of-line, but are also
;; used to search for next matches, so neither omitting "^" nor
;; replacing "^" by "\n" (to make it slightly faster) will work.

(defvar dired-re-inode-size "[ \t0-9]*")
;; Regexp for optional initial inode and file size.
;; Must match output produced by ls' -i and -s flags.

(defvar dired-re-mark "^[^ \n\r]")
;; Regexp matching a marked line.
;; Important: the match ends just after the marker.

(defvar dired-re-maybe-mark "^. ")

(defvar dired-re-dir (concat dired-re-maybe-mark dired-re-inode-size "d"))
;; Matches directory lines

(defvar dired-re-sym (concat dired-re-maybe-mark dired-re-inode-size "l"))
;; Matches symlink lines

(defvar dired-re-exe;; match ls permission string of an executable file
  (mapconcat (function
	      (lambda (x)
		(concat dired-re-maybe-mark dired-re-inode-size x)))
	     '("-[-r][-w][xs][-r][-w].[-r][-w]."
	       "-[-r][-w].[-r][-w][xs][-r][-w]."
	       "-[-r][-w].[-r][-w].[-r][-w][xst]")
	     "\\|"))

(defvar dired-re-dot "^.* \\.\\.?/?$")	; with -F, might end in `/'
;; . and .. files

(setq dired-re-month-and-time
      (let* ((month (concat "\\("
			  (mapconcat 'identity
					;; June and July are for HP-UX 9.0
				     '("Jan" "Feb" "Mar" "Apr" "May" "June?"
				       "July?" "Aug" "Sep" "Oct" "Nov" "Dec")
				     "\\|")
			  "\\)"))
	     (date "[ 0-3][0-9]")
	     (time "[012][0-9]:[0-6][0-9]")
	     (year (concat
		    "\\("
		    ;; year on IRIX, NeXT, SunOS, ULTRIX, Apollo, HP-UX, A/UX
		    " ?[12][90][0-9][0-9] ?"
		    "\\|"
		    ;; year on AIX
		    "[12][90][0-9][0-9]  "
		    "\\)"))
	     (month-date (concat "\\(" month " " date "\\)"))
	     (date-month (concat "\\(" date " " month "\\)")))
	(concat " "
		"\\("  date-month "\\|" month-date "\\)"
		" "
		"\\(" time "\\|" year "\\)"
		" ")))

;; This regexp MUST match all the way to first character of the filename.
;; You can loosen it to taste, but then you might bomb on filenames starting
;; with a space. This will have to be modified for non-english month names.

(defvar dired-subdir-regexp
  "\\([\n\r]\n\\|\\`\\). \\([^\n\r]+\\)\\(:\\)\\(\\.\\.\\.\r\\|[\n\r]\\)")
  ;; Regexp matching a maybe hidden subdirectory line in ls -lR output.
  ;; Subexpression 2 is the subdirectory proper, no trailing colon.
  ;; Subexpression 3 must end right before the \n or \r at the end of
  ;; the subdir heading.  Matches headings after indentation has been done.

(defvar dired-unhandle-add-files nil)
;; List of files that the dired handler function need not add to dired buffers.
;; This is because they have already been added, most likely in
;; dired-create-files.  This is because dired-create-files add files with
;; special markers.

;;; history variables

(defvar dired-regexp-history nil
  "History list of regular expressions used in Dired commands.")

(defvar dired-chmod-history nil
  "History of arguments to chmod in dired.")

(defvar dired-chown-history nil
  "History of arguments to chown in dired.")

(defvar dired-chgrp-history nil
  "History of arguments to chgrp in dired.")

(defvar dired-cleanup-history nil
  "History of arguments to dired-cleanup.")

(defvar dired-goto-file-history nil)
;; History for dired-goto-file and dired-goto-subdir
(put 'dired-goto-file-history 'cursor-end t) ; for gmhist

(defvar dired-history nil)
;; Catch-all history variable for dired file ops without
;; their own history.

(defvar dired-op-history-alist
  ;; alist of dired file operations and history symbols
  '((chgrp . dired-chgrp-history) (chown . dired-chown-history)
    (chmod . dired-chmod-history) ))

;;; Tell the byte-compiler that we know what we're doing.
;;; Do we?

(defvar file-name-handler-alist)
(defvar inhibit-file-name-operation)
(defvar inhibit-file-name-handlers)
(defvar efs-dired-host-type)


;;;;------------------------------------------------------------------
;;;; Utilities
;;;;------------------------------------------------------------------ 

;;; Macros
;;
;;  Macros must be defined before they are used - for the byte compiler.

(defmacro dired-get-subdir-min (elt)
  ;; Returns the value of the subdir minumum for subdir with entry ELT in
  ;; dired-subdir-alist.
  (list 'nth 1 elt))

(defmacro dired-save-excursion (&rest body)
  ;; Saves excursions of the point (not buffer) in dired buffers.
  ;; It tries to be robust against deletion of the region about the point.
  ;; Note that this assumes only dired-style deletions.
  (let ((temp-bolm (make-symbol "bolm"))
	(temp-fnlp (make-symbol "fnlp"))
	(temp-offset-bol (make-symbol "offset-bol")))
    (` (let (((, temp-bolm) (make-marker))
	     (, temp-fnlp) (, temp-offset-bol))
	 (let ((bol (save-excursion (skip-chars-backward "^\n\r") (point))))
	   (set-marker (, temp-bolm) bol)
	   (setq (, temp-offset-bol) (- (point) bol)
		 (, temp-fnlp) (memq (char-after bol) '(?\n\ ?\r))))
	 (unwind-protect
	     (progn
	       (,@ body))
	   ;; Use the marker to try to find the right line, then move to
	   ;; the proper column.
	   (goto-char (, temp-bolm))
	   (and (not (, temp-fnlp))
		(memq (char-after (point)) '(?\n ?\r))
		;; The line containing the point got deleted. Note that this
		;; logic only works if we don't delete null lines, but we never
		;; do.
		(forward-line 1)) ; don't move into a hidden line.
	   (skip-chars-forward "^\n\r" (+ (point) (, temp-offset-bol))))))))

(put 'dired-save-excursion 'lisp-indent-hook 0)

(defun dired-substitute-marker (pos old new)
  ;; Change marker, re-fontify
  (subst-char-in-region pos (1+ pos) old new)
  (dired-move-to-filename))

(defmacro dired-mark-if (predicate msg)
  ;; Mark all files for which CONDITION evals to non-nil.
  ;; CONDITION is evaluated on each line, with point at beginning of line.
  ;; MSG is a noun phrase for the type of files being marked.
  ;; It should end with a noun that can be pluralized by adding `s'.
  ;; Return value is the number of files marked, or nil if none were marked.
  (let ((temp-pt (make-symbol "pt"))
	(temp-count (make-symbol "count"))
	(temp-msg (make-symbol "msg")))
    (` (let (((, temp-msg) (, msg))
	     ((, temp-count) 0)
	     (, temp-pt) buffer-read-only)
	 (save-excursion
	   (if (, temp-msg) (message "Marking %ss..." (, temp-msg)))
	   (goto-char (point-min))
	   (while (not (eobp))
	     (if (and (, predicate)
		      (not (char-equal (char-after (point)) dired-marker-char)))
		 (progn
		   ;; Doing this rather than delete-char, insert
		   ;; avoids re-computing markers
		   (setq (, temp-pt) (point))
		   (dired-substitute-marker
		    (, temp-pt)
		    (char-after (point)) dired-marker-char)
		   (setq (, temp-count) (1+ (, temp-count)))))
	     (forward-line 1))
	   (if (, temp-msg)
	       (message "%s %s%s %s%s."
			(, temp-count)
			(, temp-msg)
			(dired-plural-s (, temp-count))
			(if (equal dired-marker-char ?\040) "un" "")
			(if (equal dired-marker-char dired-del-marker)
			    "flagged" "marked"))))
	 (and (> (, temp-count) 0) (, temp-count))))))

(defmacro dired-map-over-marks (body arg &optional show-progress)
;;  Perform BODY with point somewhere on each marked line
;;  and return a list of BODY's results.
;;  If no marked file could be found, execute BODY on the current line.
;;  If ARG is an integer, use the next ARG (or previous -ARG, if ARG<0)
;;  files instead of the marked files.
;;  If ARG is t, only apply to marked files.  If there are no marked files,
;;  the result is a noop.
;;  If ARG is otherwise non-nil, use current file instead.
;;  If optional third arg SHOW-PROGRESS evaluates to non-nil,
;;  redisplay the dired buffer after each file is processed.
;;  No guarantee is made about the position on the marked line.
;;  BODY must ensure this itself if it depends on this.
;;  Search starts at the beginning of the buffer, thus the car of the list
;;  corresponds to the line nearest to the buffer's bottom.  This
;;  is also true for (positive and negative) integer values of ARG.
;;  To avoid code explosion, BODY should not be too long as it is
;;  expanded four times.
;;
;;  Warning: BODY must not add new lines before point - this may cause an
;;  endless loop.
;;  This warning should not apply any longer, sk  2-Sep-1991 14:10.
  (let ((temp-found (make-symbol "found"))
	(temp-results (make-symbol "results"))
	(temp-regexp (make-symbol "regexp"))
	(temp-curr-pt (make-symbol "curr-pt"))
	(temp-next-position (make-symbol "next-position")))
    (` (let (buffer-read-only case-fold-search (, temp-found) (, temp-results))
	 (dired-save-excursion
	   (if (and (, arg) (not (eq (, arg) t)))
	       (if (integerp (, arg))
		   (and (not (zerop (, arg)))
			(progn;; no save-excursion, want to move point.
			  (dired-repeat-over-lines
			   arg
			   (function (lambda ()
				       (if (, show-progress) (sit-for 0))
				       (setq (, temp-results)
					     (cons (, body)
						   (, temp-results))))))
			  (if (<  (, arg) 0)
			      (nreverse (, temp-results))
			    (, temp-results))))
		 ;; non-nil, non-integer ARG means use current file:
		 (list (, body)))
	     (let (((, temp-regexp)
		    (concat "^" (regexp-quote (char-to-string
					       dired-marker-char))))
		   (, temp-curr-pt) (, temp-next-position))
	       (save-excursion
		 (goto-char (point-min))
		 ;; remember position of next marked file before BODY
		 ;; can insert lines before the just found file,
		 ;; confusing us by finding the same marked file again
		 ;; and again and...
		 (setq (, temp-next-position)
		       (and (re-search-forward (, temp-regexp) nil t)
			    (point-marker))
		       (, temp-found) (not (null (, temp-next-position))))
		 (while (, temp-next-position)
		   (setq (, temp-curr-pt) (goto-char (, temp-next-position))
			 ;; need to get next position BEFORE body
			 (, temp-next-position)
			 (and (re-search-forward (, temp-regexp) nil t)
			      (point-marker)))
		   (goto-char (, temp-curr-pt))
		   (if (, show-progress) (sit-for 0))
		   (setq (, temp-results) (cons (, body) (, temp-results)))))
	       (if (, temp-found)
		   (, temp-results)
		 ;; Do current file, unless arg is t
		 (and (not (eq (, arg) t))
		      (list (, body)))))))))))

;;; General utility functions

(defun dired-buffer-more-recently-used-p (buffer1 buffer2)
  "Return t if BUFFER1 is more recently used than BUFFER2."
  (if (equal buffer1 buffer2)
      nil
    (let ((more-recent nil)
	  (list (buffer-list)))
      (while (and list
		  (not (setq more-recent (equal buffer1 (car list))))
		  (not (equal buffer2 (car list))))
	(setq list (cdr list)))
      more-recent)))

(defun dired-file-modtime (file)
  ;; Return the modtime of FILE, which is assumed to be already expanded
  ;; by expand-file-name.
  (let ((handler (find-file-name-handler file 'dired-file-modtime)))
    (if handler
	(funcall handler 'dired-file-modtime file)
      (nth 5 (file-attributes file)))))

(defun dired-set-file-modtime (file alist)
  ;; Set the modtime for FILE in the subdir alist ALIST.
  (let ((handler (find-file-name-handler file 'dired-set-file-modtime)))
    (if handler
	(funcall handler 'dired-set-file-modtime file alist)
      (let ((elt (assoc file alist)))
	(if elt
	    (setcar (nthcdr 4 elt) (nth 5 (file-attributes file))))))))

(defun dired-map-over-marks-check (fun arg op-symbol operation
				       &optional show-progress no-confirm)
  ;; Map FUN over marked files (with second ARG like in dired-map-over-marks)
  ;; and display failures.

  ;; FUN takes zero args.  It returns non-nil (the offending object, e.g.
  ;; the short form of the filename) for a failure and probably logs a
  ;; detailed error explanation using function `dired-log'.

  ;; OP-SYMBOL is s symbol representing the operation.
  ;; eg. 'compress

  ;; OPERATION is a string describing the operation performed (e.g.
  ;; "Compress").  It is used with `dired-mark-pop-up' to prompt the user
  ;; (e.g. with `Compress * [2 files]? ') and to display errors (e.g.
  ;; `Failed to compress 1 of 2 files - type y to see why ("foo")')

  ;; SHOW-PROGRESS if non-nil means redisplay dired after each file.

  (if (or no-confirm (dired-mark-confirm op-symbol operation arg))
      (let* ((total-list;; all of FUN's return values
	      (dired-map-over-marks (funcall fun) arg show-progress))
	     (total (length total-list))
	     (failures (delq nil total-list))
	     (count (length failures)))
	(if (not failures)
	    (message "%s: %d file%s." operation total (dired-plural-s total))
	  (message "Failed to %s %d of %d file%s - type y to see why %s"
		   operation count total (dired-plural-s total)
		   ;; this gives a short list of failed files in parens
		   ;; which may be sufficient for the user even
		   ;; without typing `W' for the process' diagnostics
		   failures)
	  ;; end this bunch of errors:
	  (dired-log-summary
	   (buffer-name (current-buffer))
	   (format
	    "Failed to %s %d of %d file%s"
	    operation count total (dired-plural-s total))
	   failures)))))

(defun dired-make-switches-string (list)
;; Converts a list of cracters to a string suitable for passing to ls.
  (concat "-" (mapconcat 'char-to-string list "")))

(defun dired-make-switches-list (string)
;; Converts a string of ls switches to a list of characters.
  (delq ?- (mapcar 'identity string)))

;; Cloning replace-match to work on strings instead of in buffer:
;; The FIXEDCASE parameter of replace-match is not implemented.
(defun dired-string-replace-match (regexp string newtext
					  &optional literal global)
  ;; Replace first match of REGEXP in STRING with NEWTEXT.
  ;; If it does not match, nil is returned instead of the new string.
  ;; Optional arg LITERAL means to take NEWTEXT literally.
  ;; Optional arg GLOBAL means to replace all matches.
  (if global
        (let ((result "") (start 0) mb me)
	  (while (string-match regexp string start)
	    (setq mb (match-beginning 0)
		  me (match-end 0)
		  result (concat result
				 (substring string start mb)
				 (if literal
				     newtext
				   (dired-expand-newtext string newtext)))
		  start me))
	  (if mb			; matched at least once
	      (concat result (substring string start))
	    nil))
    ;; not GLOBAL
    (if (not (string-match regexp string 0))
	nil
      (concat (substring string 0 (match-beginning 0))
	      (if literal newtext (dired-expand-newtext string newtext))
	      (substring string (match-end 0))))))

(defun dired-expand-newtext (string newtext)
  ;; Expand \& and \1..\9 (referring to STRING) in NEWTEXT, using match data.
  ;; Note that in Emacs 18 match data are clipped to current buffer
  ;; size...so the buffer should better not be smaller than STRING.
  (let ((pos 0)
	(len (length newtext))
	(expanded-newtext ""))
    (while (< pos len)
      (setq expanded-newtext
	    (concat expanded-newtext
		    (let ((c (aref newtext pos)))
		      (if (= ?\\ c)
			  (cond ((= ?\& (setq c
					      (aref newtext
						    (setq pos (1+ pos)))))
				 (substring string
					    (match-beginning 0)
					    (match-end 0)))
				((and (>= c ?1) (<= c ?9))
				 ;; return empty string if N'th
				 ;; sub-regexp did not match:
				 (let ((n (- c ?0)))
				   (if (match-beginning n)
				       (substring string
						  (match-beginning n)
						  (match-end n))
				     "")))
				(t
				 (char-to-string c)))
			(char-to-string c)))))
      (setq pos (1+ pos)))
    expanded-newtext))

(defun dired-in-this-tree (file dir)
  ;;Is FILE part of the directory tree starting at DIR?
  (let ((len (length dir)))
    (and (>= (length file) len)
	 (string-equal (substring file 0 len) dir))))

(defun dired-tree-lessp (dir1 dir2)
  ;; Lexicographic order on pathname components, like `ls -lR':
  ;; DIR1 < DIR2 iff DIR1 comes *before* DIR2 in an `ls -lR' listing,
  ;;   i.e., iff DIR1 is a (grand)parent dir of DIR2,
  ;;   or DIR1 and DIR2 are in the same parentdir and their last
  ;;   components are string-lessp.
  ;; Thus ("/usr/" "/usr/bin") and ("/usr/a/" "/usr/b/") are tree-lessp.
  ;; string-lessp could arguably be replaced by file-newer-than-file-p
  ;;   if dired-internal-switches contained `t'.
  (let ((dir1 (file-name-as-directory dir1))
	(dir2 (file-name-as-directory dir2))
	(start1 1)
	(start2 1)
	comp1 comp2 end1 end2)
    (while (progn
	     (setq end1 (string-match "/" dir1 start1)
		   comp1 (substring dir1 start1 end1)
		   end2 (string-match "/" dir2 start2)
		   comp2 (substring dir2 start2 end2))
		   (and end1 end2 (string-equal comp1 comp2)))
      (setq start1 (1+ end1)
	    start2 (1+ end2)))
    (if (eq (null end1) (null end2))
	(string-lessp comp1 comp2)
      (null end1))))

;; So that we can support case-insensitive systems.
(fset 'dired-file-name-lessp 'string-lessp)


;;;; ------------------------------------------------------------------
;;;; Initializing Dired
;;;; ------------------------------------------------------------------

;;; Set the minor mode alist

(or (equal (assq 'dired-sort-mode minor-mode-alist)
	   '(dired-sort-mode dired-sort-mode))
    ;; Test whether this has already been done in case dired is reloaded
    ;; There may be several elements with dired-sort-mode as car.
    (setq minor-mode-alist
	  ;; cons " Omit" in first, so that it doesn't
	  ;; get stuck between the directory and sort mode on the
	  ;; mode line.
	  (cons '(dired-sort-mode dired-sort-mode)
		(cons '(dired-subdir-omit " Omit")
		      (cons '(dired-marker-stack dired-marker-string)
			    minor-mode-alist)))))

;;; Keymaps

(defvar dired-mode-map nil
  "Local keymap for dired-mode buffers.")
(defvar dired-regexp-map nil
  "Dired keymap for commands that use regular expressions.")
(defvar dired-diff-map nil
  "Dired keymap for diff and related commands.")
(defvar dired-subdir-map nil
  "Dired keymap for commands that act on subdirs, or the files within them.")

(defvar dired-keymap-grokked nil
  "Set to t after dired has grokked the global keymap.")

(defun dired-key-description (cmd &rest prefixes)
  ;; Return a key description string for a menu.  If prefixes are given,
  ;; they should be either strings, integers, or 'universal-argument.
  (let ((key (where-is-internal cmd dired-mode-map t)))
    (if key
	(key-description
	 (apply 'vconcat
		(append
		 (mapcar
		  (function
		   (lambda (x)
		     (cond ((eq x 'universal-argument)
			    (where-is-internal 'universal-argument
					       dired-mode-map t))
			   ((integerp x) (int-to-string x))
			   (t x))))
		  prefixes)
		 (list key))))
      "")))

(defun dired-grok-keys (to-command from-command)
  ;; Assigns to TO-COMMAND the keys for the global binding of FROM-COMMAND.
  ;; Does not clobber anything in the local keymap.  In emacs 19 should
  ;; use substitute-key-definition, but I believe that this will
  ;; clobber things in the local map.
  (let ((keys (where-is-internal from-command)))
    (while keys
      (condition-case nil
	  (if (eq (global-key-binding (car keys)) (key-binding (car keys)))
	      (local-set-key (car keys) to-command))
	(error nil))
      (setq keys (cdr keys)))))

(defun dired-grok-keymap ()
  ;; Initialize the dired keymaps.
  ;; This is actually done the first time that dired-mode runs.
  ;; We do it this late, to be sure that the user's global-keymap has
  ;; stabilized.
  (if dired-keymap-grokked
      () ; we've done it
    ;; Watch out for dired being invoked from the command line.
    ;; This is a bit kludgy, but so is the emacs startup sequence IMHO.
    (if (and term-setup-hook (boundp 'command-line-args-left))
	(progn
	  (if (string-equal "18." (substring emacs-version 0 3))
	      (funcall term-setup-hook)
	    (run-hooks 'term-setup-hook))
	  (setq term-setup-hook nil)))
    (setq dired-keymap-grokked t)
    (run-hooks 'dired-setup-keys-hook)
    (dired-grok-keys 'dired-next-line 'next-line)
    (dired-grok-keys 'dired-previous-line 'previous-line)
    (dired-grok-keys 'dired-undo 'undo)
    (dired-grok-keys 'dired-undo 'advertised-undo)
    (dired-grok-keys 'dired-scroll-up 'scroll-up)
    (dired-grok-keys 'dired-scroll-down 'scroll-down)
    (dired-grok-keys 'dired-beginning-of-buffer 'beginning-of-buffer)
    (dired-grok-keys 'dired-end-of-buffer 'end-of-buffer)
    (dired-grok-keys 'dired-next-subdir 'forward-paragraph)
    (dired-grok-keys 'dired-prev-subdir 'backward-paragraph)))

;; The regexp-map is used for commands using regexp's.
(if dired-regexp-map
    ()
  (setq dired-regexp-map (make-sparse-keymap))
  (define-key dired-regexp-map "C" 'dired-do-copy-regexp)
  ;; Not really a regexp, but does transform file names.
  (define-key dired-regexp-map "D" 'dired-downcase)
  (define-key dired-regexp-map "H" 'dired-do-hardlink-regexp)
  (define-key dired-regexp-map "R" 'dired-do-rename-regexp)
  (define-key dired-regexp-map "S" 'dired-do-symlink-regexp)
  (define-key dired-regexp-map "U" 'dired-upcase)
  (define-key dired-regexp-map "Y" 'dired-do-relsymlink-regexp)
  (define-key dired-regexp-map "c" 'dired-cleanup)
  (define-key dired-regexp-map "d" 'dired-flag-files-regexp)
  (define-key dired-regexp-map "e" 'dired-mark-extension)
  (define-key dired-regexp-map "m" 'dired-mark-files-regexp)
  (define-key dired-regexp-map "o" 'dired-add-omit-regexp)
  (define-key dired-regexp-map "x" 'dired-flag-extension)) ; a string, rather
					; than a regexp.

(if dired-diff-map
    ()
  (setq dired-diff-map (make-sparse-keymap))
  (define-key dired-diff-map "d" 'dired-diff)
  (define-key dired-diff-map "b" 'dired-backup-diff)
  (define-key dired-diff-map "m" 'dired-emerge)
  (define-key dired-diff-map "a" 'dired-emerge-with-ancestor)
  (define-key dired-diff-map "e" 'dired-ediff)
  (define-key dired-diff-map "p" 'dired-epatch))

(if dired-subdir-map
    ()
  (setq dired-subdir-map (make-sparse-keymap))
  (define-key dired-subdir-map "n" 'dired-redisplay-subdir)
  (define-key dired-subdir-map "m" 'dired-mark-subdir-files)
  (define-key dired-subdir-map "d" 'dired-flag-subdir-files)
  (define-key dired-subdir-map "z" 'dired-compress-subdir-files))

(fset 'dired-regexp-prefix dired-regexp-map)
(fset 'dired-diff-prefix dired-diff-map)
(fset 'dired-subdir-prefix dired-subdir-map)
(fset 'efs-dired-prefix (function (lambda ()
				    (interactive)
				    (error "efs-dired not loaded yet"))))

;; the main map
(if dired-mode-map
    nil
  ;; Force `f' rather than `e' in the mode doc:
  (fset 'dired-advertised-find-file 'dired-find-file)
  (fset 'dired-advertised-next-subdir 'dired-next-subdir)
  (fset 'dired-advertised-prev-subdir 'dired-prev-subdir)
  (setq dired-mode-map (make-keymap))
  (suppress-keymap dired-mode-map)
  ;; Commands to mark certain categories of files
  (define-key dired-mode-map "~" 'dired-flag-backup-files)
  (define-key dired-mode-map "#" 'dired-flag-auto-save-files)
  (define-key dired-mode-map "*" 'dired-mark-executables)
  (define-key dired-mode-map "." 'dired-clean-directory)
  (define-key dired-mode-map "/" 'dired-mark-directories)
  (define-key dired-mode-map "@" 'dired-mark-symlinks)
  (define-key dired-mode-map "," 'dired-mark-rcs-files)
  (define-key dired-mode-map "\M-(" 'dired-mark-sexp)
  (define-key dired-mode-map "\M-d" 'dired-mark-files-from-other-dired-buffer)
  (define-key dired-mode-map "\M-c" 'dired-mark-files-compilation-buffer)
  ;; Upper case keys (except ! and &) for operating on the marked files
  (define-key dired-mode-map "A" 'dired-do-tags-search)
  (define-key dired-mode-map "B" 'dired-do-byte-compile)
  (define-key dired-mode-map "C" 'dired-do-copy)
  (define-key dired-mode-map "E" 'dired-do-grep)
  (define-key dired-mode-map "F" 'dired-do-find-file)
  (define-key dired-mode-map "G" 'dired-do-chgrp)
  (define-key dired-mode-map "H" 'dired-do-hardlink)
  (define-key dired-mode-map "I" 'dired-do-insert-subdir)
  (define-key dired-mode-map "K" 'dired-do-kill-file-lines)
  (define-key dired-mode-map "L" 'dired-do-load)
  (define-key dired-mode-map "M" 'dired-do-chmod)
  (define-key dired-mode-map "N" 'dired-do-redisplay)
  (define-key dired-mode-map "O" 'dired-do-chown)
  (define-key dired-mode-map "P" 'dired-do-print)
  (define-key dired-mode-map "Q" 'dired-do-tags-query-replace)
  (define-key dired-mode-map "R" 'dired-do-rename)
  (define-key dired-mode-map "S" 'dired-do-symlink)
  (define-key dired-mode-map "T" 'dired-do-total-size)
  (define-key dired-mode-map "U" 'dired-do-uucode)
  (define-key dired-mode-map "W" 'dired-copy-filenames-as-kill)
  (define-key dired-mode-map "X" 'dired-do-delete)
  (define-key dired-mode-map "Y" 'dired-do-relsymlink)
  (define-key dired-mode-map "Z" 'dired-do-compress)
  (define-key dired-mode-map "!" 'dired-do-shell-command)
  (define-key dired-mode-map "&" 'dired-do-background-shell-command)
  ;; Make all regexp commands share a `%' prefix:
  (define-key dired-mode-map "%" 'dired-regexp-prefix)
  ;; Lower keys for commands not operating on all the marked files
  (define-key dired-mode-map "a" 'dired-apropos)
  (define-key dired-mode-map "c" 'dired-change-marks)
  (define-key dired-mode-map "d" 'dired-flag-file-deletion)
  (define-key dired-mode-map "\C-d" 'dired-flag-file-deletion-backup)
  (define-key dired-mode-map "e" 'dired-find-file)
  (define-key dired-mode-map "f" 'dired-advertised-find-file)
  (define-key dired-mode-map "g" 'revert-buffer)
  (define-key dired-mode-map "h" 'dired-describe-mode)
  (define-key dired-mode-map "i" 'dired-maybe-insert-subdir)
  (define-key dired-mode-map "k" 'dired-kill-subdir)
  (define-key dired-mode-map "m" 'dired-mark)
  (define-key dired-mode-map "o" 'dired-find-file-other-window)
  (define-key dired-mode-map "q" 'dired-quit)
  (define-key dired-mode-map "r" 'dired-read-mail)
  (define-key dired-mode-map "s" 'dired-sort-toggle-or-edit)
  (define-key dired-mode-map "t" 'dired-get-target-directory)
  (define-key dired-mode-map "u" 'dired-unmark)
  (define-key dired-mode-map "v" 'dired-view-file)
  (define-key dired-mode-map "w" (if (fboundp 'find-file-other-frame)
				     'dired-find-file-other-frame
				   'dired-find-file-other-window))
  (define-key dired-mode-map "x" 'dired-expunge-deletions)
  (define-key dired-mode-map "y" 'dired-why)
  (define-key dired-mode-map "+" 'dired-create-directory)
  (define-key dired-mode-map "`" 'dired-recover-file)
  ;; dired-jump-back Should be in the global map, but put them here
  ;; too anyway.
  (define-key dired-mode-map "\C-x\C-j" 'dired-jump-back)
  (define-key dired-mode-map "\C-x4\C-j" 'dired-jump-back-other-window)
  (define-key dired-mode-map "\C-x5\C-j" 'dired-jump-back-other-frame)
  ;; Comparison commands
  (define-key dired-mode-map "=" 'dired-diff-prefix)
  ;; moving
  (define-key dired-mode-map "<" 'dired-prev-dirline)
  (define-key dired-mode-map ">" 'dired-next-dirline)
  (define-key dired-mode-map " "  'dired-next-line)
  (define-key dired-mode-map "n" 'dired-next-line)
  (define-key dired-mode-map "\C-n" 'dired-next-line)
  (define-key dired-mode-map "p" 'dired-previous-line)
  (define-key dired-mode-map "\C-p" 'dired-previous-line)
  (define-key dired-mode-map "\C-v" 'dired-scroll-up)
  (define-key dired-mode-map "\M-v" 'dired-scroll-down)
  (define-key dired-mode-map "\M-<" 'dired-beginning-of-buffer)
  (define-key dired-mode-map "\M->" 'dired-end-of-buffer)
  (define-key dired-mode-map "\C-m" 'dired-advertised-find-file)
  (define-key dired-mode-map [(control return)] 'dired-find-alternate-file)
  ;; motion by subdirectories
  (define-key dired-mode-map "^" 'dired-up-directory)
  (define-key dired-mode-map "\M-\C-u" 'dired-up-directory)
  (define-key dired-mode-map "\M-\C-d" 'dired-down-directory)
  (define-key dired-mode-map "\M-\C-n" 'dired-advertised-next-subdir)
  (define-key dired-mode-map "\M-\C-p" 'dired-advertised-prev-subdir)
  (define-key dired-mode-map "\C-j" 'dired-goto-subdir)
  ;; move to marked files
  (define-key dired-mode-map "\M-p" 'dired-prev-marked-file)
  (define-key dired-mode-map "\M-n" 'dired-next-marked-file)
  ;; hiding
  (define-key dired-mode-map "$" 'dired-hide-subdir)
  (define-key dired-mode-map "\M-$" 'dired-hide-all)
  ;; omitting
  (define-key dired-mode-map "\C-o" 'dired-omit-toggle)
  ;; markers
  (define-key dired-mode-map "\(" 'dired-set-marker-char)
  (define-key dired-mode-map "\)" 'dired-restore-marker-char)
  (define-key dired-mode-map "'" 'dired-marker-stack-left)
  (define-key dired-mode-map "\\" 'dired-marker-stack-right)
  ;; misc
  (define-key dired-mode-map "\C-i" 'dired-mark-prefix)
  (define-key dired-mode-map "?" 'dired-summary)
  (define-key dired-mode-map "\177" 'dired-backup-unflag)
  (define-key dired-mode-map "\C-_" 'dired-undo)
  (define-key dired-mode-map "\C-xu" 'dired-undo)
  (define-key dired-mode-map "\M-\C-?" 'dired-unmark-all-files)
  (define-key dired-mode-map "\C-c\C-f" 'dired-do-find-marked-files)
  ;; The subdir map
  (define-key dired-mode-map "|" 'dired-subdir-prefix)
  ;; efs submap
  (define-key dired-mode-map "\M-e" 'efs-dired-prefix))



;;;;------------------------------------------------------------------
;;;; The dired command
;;;;------------------------------------------------------------------

;;; User commands:
;;; All of these commands should have a binding in the global keymap.

;;;###autoload (define-key ctl-x-map "d" 'dired)
;;;###autoload
(defun dired (dirname &optional switches)
  "\"Edit\" directory DIRNAME--delete, rename, print, etc. some files in it.
Optional second argument SWITCHES specifies the `ls' options used.
\(Interactively, use a prefix argument to be able to specify SWITCHES.)
Dired displays a list of files in DIRNAME (which may also have
shell wildcards appended to select certain files).  If DIRNAME is a cons,
its first element is taken as the directory name and the resr as an explicit
list of files to make directory entries for.
\\<dired-mode-map>\
You can move around in it with the usual commands.
You can flag files for deletion with \\[dired-flag-file-deletion] and then
delete them by typing \\[dired-expunge-deletions].
Type \\[dired-describe-mode] after entering dired for more info.

If DIRNAME is already in a dired buffer, that buffer is used without refresh."
  ;; Cannot use (interactive "D") because of wildcards.
  (interactive (dired-read-dir-and-switches ""))
  (switch-to-buffer (dired-noselect dirname switches)))

;;;###autoload (define-key ctl-x-4-map "d" 'dired-other-window)
;;;###autoload
(defun dired-other-window (dirname &optional switches)
  "\"Edit\" directory DIRNAME.  Like `dired' but selects in another window."
  (interactive (dired-read-dir-and-switches "in other window "))
  (switch-to-buffer-other-window (dired-noselect dirname switches)))

;;;###autoload (define-key ctl-x-5-map "d" 'dired-other-frame)
;;;###autoload
(defun dired-other-frame (dirname &optional switches)
  "\"Edit\" directory DIRNAME.  Like `dired' but makes a new frame."
  (interactive (dired-read-dir-and-switches "in other frame "))
  (switch-to-buffer-other-frame (dired-noselect dirname switches)))

;;;###autoload
(defun dired-noselect (dir-or-list &optional switches)
  "Like `dired' but returns the dired buffer as value, does not select it."
  (or dir-or-list (setq dir-or-list (expand-file-name default-directory)))
  ;; This loses the distinction between "/foo/*/" and "/foo/*" that
  ;; some shells make:
  (let (dirname)
    (if (consp dir-or-list)
	(setq dirname (car dir-or-list))
      (setq dirname dir-or-list))
    (setq dirname (expand-file-name (directory-file-name dirname)))
    (if (file-directory-p dirname)
	(setq dirname (file-name-as-directory dirname)))
    (if (consp dir-or-list)
	(setq dir-or-list (cons dirname (cdr dir-or-list)))
      (setq dir-or-list dirname))
    (dired-internal-noselect dir-or-list switches)))

;; Adapted from code by wurgler@zippysun.math.uakron.edu (Tom Wurgler).
;;;###autoload (define-key ctl-x-map "\C-j" 'dired-jump-back)
;;;###autoload
(defun dired-jump-back ()
  "Jump back to dired.
If in a file, dired the current directory and move to file's line.
If in dired already, pop up a level and goto old directory's line.
In case the proper dired file line cannot be found, refresh the dired
  buffer and try again."
  (interactive)
  (let* ((file (if (eq major-mode 'dired-mode)
		   (directory-file-name (dired-current-directory))
		 buffer-file-name))
	 (dir (if file
		  (file-name-directory file)
		default-directory)))
    (dired dir)
    (if file (dired-really-goto-file file))))

;;;###autoload (define-key ctl-x-4-map "\C-j" 'dired-jump-back-other-window)
;;;###autoload
(defun dired-jump-back-other-window ()
  "Like \\[dired-jump-back], but to other window."
  (interactive)
  (let* ((file (if (eq major-mode 'dired-mode)
		   (directory-file-name (dired-current-directory))
		 buffer-file-name))
	 (dir (if file
		  (file-name-directory file)
		default-directory)))
    (dired-other-window dir)
    (if file (dired-really-goto-file file))))

;;;###autoload (define-key ctl-x-5-map "\C-j" 'dired-jump-back-other-frame)
;;;###autoload
(defun dired-jump-back-other-frame ()
  "Like \\[dired-jump-back], but in another frame."
  (interactive)
  (let* ((file (if (eq major-mode 'dired-mode)
		   (directory-file-name (dired-current-directory))
		 buffer-file-name))
	 (dir (if file
		  (file-name-directory file)
		default-directory)))
    (dired-other-frame dir)
    (if file (dired-really-goto-file file))))

;;; Dired mode

;; Dired mode is suitable only for specially formatted data.
(put 'dired-mode 'mode-class 'special)

(defun dired-mode (&optional dirname switches)
  "\\<dired-mode-map>Dired mode is for \"editing\" directory trees.

For a simple one-line help message, type \\[dired-summary]
For a moderately detailed description of dired mode, type \\[dired-describe-mode]
For the full dired info tree, type \\[universal-argument] \\[dired-describe-mode]"
  ;; Not to be called interactively (e.g. dired-directory will be set
  ;; to default-directory, which is wrong with wildcards).
  (kill-all-local-variables)
  (use-local-map dired-mode-map)
  (setq major-mode 'dired-mode
	mode-name "Dired"
	case-fold-search nil
	buffer-read-only t
	selective-display t		; for subdirectory hiding
	selective-display-ellipses nil  ; for omit toggling
	mode-line-buffer-identification '("Dired: %12b")
	mode-line-modified (format dired-mode-line-modified "--" "--" "-")
	dired-directory (expand-file-name (or dirname default-directory))
	dired-internal-switches (dired-make-switches-list
			       (or switches dired-listing-switches)))
  (dired-advertise)			; default-directory is already set
  (set (make-local-variable 'revert-buffer-function)
       (function dired-revert))
  (set (make-local-variable 'default-directory-function)
       'dired-current-directory)
  (set (make-local-variable 'page-delimiter)
       "\n\n")
  (set (make-local-variable 'list-buffers-directory)
       dired-directory)
  ;; Will only do something in Emacs 19.
  (add-hook (make-local-variable 'kill-buffer-hook)
	    'dired-unadvertise-current-buffer)
  ;; Same here
  (if window-system
      (add-hook (make-local-variable 'post-command-hook)
		(function
		 (lambda ()
		   (if (memq this-command dired-modeline-tracking-cmds)
		       (dired-update-mode-line t))))))
  (dired-sort-other dired-internal-switches t)
  (dired-hack-local-variables)
  (run-hooks 'dired-mode-hook)
  ;; Run this after dired-mode-hook, in case that hook makes changes to
  ;; the keymap.
  (dired-grok-keymap))

;;; Internal functions for starting dired

(defun dired-read-dir-and-switches (str)
  ;; For use in interactive.
  (reverse (list
	    (if current-prefix-arg
		(read-string "Dired listing switches: "
			     dired-listing-switches))
	    (let ((default-directory (default-directory)))
	      (read-file-name (format "Dired %s(directory): " str)
			      nil default-directory nil)))))

(defun dired-hack-local-variables ()
  "Parse, bind or evaluate any local variables for current dired buffer.
See variable `dired-local-variables-file'."
  (if (and dired-local-variables-file
	   (file-exists-p dired-local-variables-file))
      (let (buffer-read-only opoint )
	(save-excursion
	  (goto-char (point-max))
	  (setq opoint (point-marker))
	  (insert "\^L\n")
	  (insert-file-contents dired-local-variables-file))
	(let ((buffer-file-name dired-local-variables-file))
	  (condition-case err
	      (hack-local-variables)
	    (error (message "Error in dired-local-variables-file: %s" err)
		   (sit-for 1))))
	;; Must delete it as (eobp) is often used as test for last
	;; subdir in dired.el.
	(delete-region opoint (point-max))
	(set-marker opoint nil))))

;; Separate function from dired-noselect for the sake of dired-vms.el.
(defun dired-internal-noselect (dir-or-list &optional switches mode)
  ;; If there is an existing dired buffer for DIRNAME, just leave
  ;; buffer as it is (don't even call dired-revert).
  ;; This saves time especially for deep trees or with efs.
  ;; The user can type `g'easily, and it is more consistent with find-file.
  ;; But if SWITCHES are given they are probably different from the
  ;; buffer's old value, so call dired-sort-other, which does
  ;; revert the buffer.
  ;; If the user specifies a directory with emacs startup, eg.
  ;; emacs ~, dir-or-list may be unexpanded at this point.

  (let* ((dirname (expand-file-name (if (consp dir-or-list)
					(car dir-or-list)
				      dir-or-list)))
	 (buffer (dired-find-buffer-nocreate dir-or-list mode))
	 ;; note that buffer already is in dired-mode, if found
	 (new-buffer-p (not buffer))
	 (old-buf (current-buffer))
	 wildcard)
    (or buffer
	(let ((default-major-mode 'fundamental-mode))
	  ;; We don't want default-major-mode to run hooks and set auto-fill
	  ;; or whatever, now that dired-mode does not
	  ;; kill-all-local-variables any longer.
	  (setq buffer (create-file-buffer (directory-file-name dirname)))))
    (set-buffer buffer)
    (if (not new-buffer-p)		; existing buffer ...
	(progn
	  (if switches
	      (dired-sort-other
	       (if (stringp switches)
		   (dired-make-switches-list switches)
		 switches)))
	  (if dired-verify-modtimes (dired-verify-modtimes))
	  (if (and dired-find-subdir
		   (not (string-equal (dired-current-directory)
				      (file-name-as-directory dirname))))
	      (dired-initial-position dirname)))
      ;; Else a new buffer
      (if (file-directory-p dirname)
	  (setq default-directory dirname
		wildcard (consp dir-or-list))
	(setq default-directory (file-name-directory dirname)
	      wildcard t))
      (or switches (setq switches dired-listing-switches))
      (dired-mode dirname switches)
      ;; default-directory and dired-internal-switches are set now
      ;; (buffer-local), so we can call dired-readin:
      (let ((failed t))
	(unwind-protect
	    (progn (dired-readin dir-or-list buffer wildcard)
		   (setq failed nil))
	  ;; dired-readin can fail if parent directories are inaccessible.
	  ;; Don't leave an empty buffer around in that case.
	  (if failed (kill-buffer buffer))))
      ;; No need to narrow since the whole buffer contains just
      ;; dired-readin's output, nothing else.  The hook can
      ;; successfully use dired functions (e.g. dired-get-filename)
      ;; as the subdir-alist has been built in dired-readin.
      (run-hooks 'dired-after-readin-hook)
      ;; I put omit-expunge after the dired-after-readin-hook
      ;; in case that hook marks files. Does this make sense? Also, users
      ;; might want to set dired-omit-files in some incredibly clever
      ;; way depending on the contents of the directory... I don't know...
      (if dired-omit-files
	  (dired-omit-expunge nil t))
      (goto-char (point-min))
      (dired-initial-position dirname))
    (set-buffer old-buf)
    buffer))

(defun dired-find-buffer-nocreate (dir-or-list &optional mode)
  ;; Returns a dired buffer for DIR-OR-LIST. DIR-OR-LIST may be wildcard,
  ;; or a directory and alist of files.
  ;; If dired-find-subdir is non-nil, is satisfied with a dired
  ;; buffer containing DIR-OR-LIST as a subdirectory. If there is more
  ;; than one candidate, returns the most recently used.
  (if dired-find-subdir
      (let ((buffers (sort (delq (current-buffer)
				 (dired-buffers-for-dir dir-or-list t))
			   (function dired-buffer-more-recently-used-p))))
	(or (car buffers)
	    ;; Couldn't find another buffer. Will the current one do?
	    ;; It is up dired-initial-position to actually go to the subdir.
	    (and (or (equal dir-or-list dired-directory) ; covers wildcards
		     (and (stringp dir-or-list)
			  (not (string-equal
				dir-or-list
				(expand-file-name default-directory)))
			  (assoc (file-name-as-directory dir-or-list)
				 dired-subdir-alist)))
		 (current-buffer))))
    ;; Else just look through the buffer list.
    (let (found (blist (buffer-list)))
      (or mode (setq mode 'dired-mode))
      (save-excursion
	(while blist
	  (set-buffer (car blist))
	  (if (and (eq major-mode mode)
		   (equal dired-directory dir-or-list))
	      (setq found (car blist)
		    blist nil)
	    (setq blist (cdr blist)))))
      found)))

(defun dired-initial-position (dirname)
  ;; Where point should go in a new listing of DIRNAME.
  ;; Point assumed at beginning of new subdir line.
  (end-of-line)
  (if dired-find-subdir (dired-goto-subdir dirname))
  (if dired-trivial-filenames (dired-goto-next-nontrivial-file))
  (dired-update-mode-line t))

(defun dired-readin (dir-or-list buffer &optional wildcard)
  ;; Read in a new dired buffer
  ;; dired-readin differs from dired-insert-subdir in that it accepts
  ;; wildcards, erases the buffer, and builds the subdir-alist anew
  ;; (including making it buffer-local and clearing it first).
  ;; default-directory and dired-internal-switches must be buffer-local
  ;; and initialized by now.
  ;; Thus we can test (equal default-directory dirname) instead of
  ;; (file-directory-p dirname) and save a filesystem transaction.
  ;; This is wrong, if dired-before-readin-hook changes default-directory
  ;; Also, we can run this hook which may want to modify the switches
  ;; based on default-directory, e.g. with efs to a SysV host
  ;; where ls won't understand -Al switches.
  (let (dirname other-dirs)
    (if (consp dir-or-list)
	(setq dir-or-list (dired-frob-dir-list dir-or-list)
	      other-dirs (cdr dir-or-list)
	      dir-or-list (car dir-or-list)
	      dirname (car dir-or-list))
      (setq dirname dir-or-list))
    (setq dirname (expand-file-name dirname))
    (if (consp dir-or-list)
	(setq dir-or-list (cons dirname (cdr dir-or-list))))
    (save-excursion
      (set-buffer buffer)
      (run-hooks 'dired-before-readin-hook)
      (message "Reading directory %s..." dirname)
      (let (buffer-read-only)
	(widen)
	(erase-buffer)
	(dired-readin-insert dir-or-list wildcard)
	;; We need this to make the root dir have a header line as all
	;; other subdirs have:
	(goto-char (point-min))
	(dired-insert-headerline (expand-file-name default-directory)))
      (message "Reading directory %s...done" dirname)
      (set-buffer-modified-p nil)
      ;; Must first make alist buffer local and set it to nil because
      ;; dired-build-subdir-alist will call dired-clear-alist first
      (setq dired-subdir-alist nil)
      (if (memq ?R dired-internal-switches)
	  (dired-build-subdir-alist)
	;; no need to parse the buffer if listing is not recursive
	(dired-simple-subdir-alist))
      (if other-dirs
	  (mapcar
	   (function
	    (lambda (x)
	      (if (dired-in-this-tree (car x) dirname)
		  (dired-insert-subdir x))))
	   other-dirs)))))
  
;;; Subroutines of dired-readin

(defun dired-readin-insert (dir-or-list &optional wildcard)
  ;; Just insert listing for the passed-in directory or
  ;; directory-and-file list, assuming a clean buffer.
  (let* ((switches (dired-make-switches-string dired-internal-switches))
	 (dir-is-list (consp dir-or-list))
	 (dirname (if dir-is-list (car dir-or-list) dir-or-list)))
    (if wildcard
	(progn
	  (or (file-readable-p
	       (if dir-is-list
		   dirname
		 (directory-file-name (file-name-directory dirname))))
	      (error "Directory %s inaccessible or nonexistent" dirname))
	  ;; else assume it contains wildcards
	  (dired-insert-directory dir-or-list switches t)
	  (save-excursion
	    ;; insert wildcard instead of total line:
	    (goto-char (point-min))
	    (if dir-is-list
		(insert "list wildcard\n")
	      (insert "wildcard " (file-name-nondirectory dirname) "\n"))))
      (dired-insert-directory dir-or-list switches nil t))))

(defun dired-insert-directory (dir-or-list switches &optional wildcard full-p)
  ;; Do the right thing whether dir-or-list is atomic or not.  If it is,
  ;; insert all files listed in the cdr -- the car is the passed-in directory
  ;; list.
  (let ((opoint (point))
	(insert-directory-program dired-ls-program) end)
    (if (consp dir-or-list)
	(mapcar
	 (function
	  (lambda (x)
	    (insert-directory x switches wildcard)))
	 (cdr dir-or-list))
      (insert-directory dir-or-list switches wildcard full-p))
    (setq end (point-marker))
    (dired-indent-listing opoint end)
    (dired-insert-set-properties opoint end))
  (setq dired-directory dir-or-list))

(defun dired-frob-dir-list (dir-list)
  (let* ((top (file-name-as-directory (expand-file-name (car dir-list))))
	 (tail (cdr dir-list))
	 (result (list (list top)))
	 elt dir)
    (setq tail
	  (mapcar
	   (function
	    (lambda (x)
	      (directory-file-name (expand-file-name x top))))
	   tail))
    (while tail
      (setq dir (file-name-directory (car tail)))
      (if (setq elt (assoc dir result))
	  (nconc elt (list (car tail)))
	(nconc result (list (list dir (car tail)))))
      (setq tail (cdr tail)))
    result))

(defun dired-insert-headerline (dir);; also used by dired-insert-subdir
  ;; Insert DIR's headerline with no trailing slash, exactly like ls
  ;; would, and put cursor where dired-build-subdir-alist puts subdir
  ;; boundaries.
  (if (fboundp 'dired-insert-set-headerline-properties)
      ;; code activate `headerline popup menu'
      (save-excursion (prog1 
			  (save-excursion 
			    (insert "  " (directory-file-name dir) ":\n"))
			(dired-insert-set-headerline-properties)
			))
    ;; Original code
    (save-excursion (insert "  " (directory-file-name dir) ":\n"))))

(defun dired-verify-modtimes ()
  ;; Check the modtimes of all subdirs.
  (let ((alist dired-subdir-alist)
	on-disk in-mem badies)
    (while alist
      (and (setq in-mem (nth 4 (car alist)))
	   (setq on-disk (dired-file-modtime (car (car alist))))
	   (not (equal in-mem on-disk))
	   (setq badies (cons (cons (car (car alist))
				    (nth 3 (car alist)))
			      badies)))
      (setq alist (cdr alist)))
    (and badies
	 (let* ((ofile (dired-get-filename nil t))
		(osub (and (null ofile) (dired-get-subdir)))
		(opoint (point))
		(ocol (current-column)))
	   (unwind-protect
	       (and
		(or (memq 'revert-subdirs dired-no-confirm)
		    (save-window-excursion
		      (let ((flist (mapcar
				    (function
				     (lambda (f)
				       (dired-abbreviate-file-name (car f))))
				    badies)))
			(switch-to-buffer (current-buffer))
		       (dired-mark-pop-up
			"*Stale Subdirectories*" 'revert-subdirs
			flist 'y-or-n-p
			(if (= (length flist) 1)
			    (concat "Subdirectory " (car flist)
				    " has changed on disk.  Re-list? ")
			  "Subdirectories have changed on disk.  Re-list? "))
		       )))
		(while badies
		  (dired-insert-subdir (car (car badies))
				       (cdr (car badies)) nil t)
		  (setq badies (cdr badies))))
	     ;; We can't use dired-save-excursion here, because we are
	     ;; rewriting the entire listing, and not just changing a single
	     ;; file line.
	     (or (if ofile
		     (dired-goto-file ofile)
		   (if osub
		       (dired-goto-subdir osub)))
		 (progn
		   (goto-char opoint)
		   (beginning-of-line)
		   (skip-chars-forward "^\n\r" (+ (point) ocol))))
	     (dired-update-mode-line t)
	     (dired-update-mode-line-modified t))))))

(defun dired-indent-listing (start end)
  ;; Indent a dired listing.
  (let (indent-tabs-mode)
    (indent-rigidly start end 2)
    ;; Quote any null lines that shouldn't be.
    (save-excursion
      (goto-char start)
      (while (search-forward "\n\n" end t)
	(forward-char -2)
	(if (looking-at dired-subdir-regexp)
	    (goto-char (match-end 3))
	  (progn
	    (forward-char 1)
	    (insert " ")))))))


;;;; ------------------------------------------------------------
;;;; Reverting a dired buffer, or specific file lines within it.
;;;; ------------------------------------------------------------

(defun dired-revert (&optional arg noconfirm)
  ;; Reread the dired buffer.  Must also be called after
  ;; dired-internal-switches have changed.
  ;; Should not fail even on completely garbaged buffers.
  ;; Preserves old cursor, marks/flags, hidden-p.
  (widen)				; just in case user narrowed
  (let ((opoint (point))
	(ofile (dired-get-filename nil t))
	(hidden-subdirs (dired-remember-hidden))
	;; switches for top-level dir
	(oswitches (or (nth 3 (nth (1- (length dired-subdir-alist))
				   dired-subdir-alist))
		       (delq ?R (copy-sequence dired-internal-switches))))
	;; all other subdirs
	(old-subdir-alist (cdr (reverse dired-subdir-alist)))
	(omitted-subdirs (dired-remember-omitted))
	;; do this after dired-remember-hidden, since this unhides
	(mark-alist (dired-remember-marks (point-min) (point-max)))
	(kill-files-p (save-excursion
			(goto-char (point))
			(search-forward
			 (concat (char-to-string ?\r)
				 (regexp-quote
				  (char-to-string
				   dired-kill-marker-char)))
			 nil t)))
	buffer-read-only)
    ;; This is bogus, as it will not handle all the ways that efs uses cache.
    ;; Better to just use the fact that revert-buffer-function is a
    ;; buffer-local variable, and reset it to something that knows about
    ;; cache.
    ;; (dired-uncache
    ;;   (if (consp dired-directory) (car dired-directory) dired-directory))
    ;; treat top level dir extra (it may contain wildcards)
    (let ((dired-after-readin-hook nil)
	  ;; don't run that hook for each subdir...
	  (dired-omit-files nil)
	  (dired-internal-switches oswitches))
      (dired-readin dired-directory (current-buffer)
		    ;; Don't test for wildcards by checking string=
		    ;; default-directory and dired-directory
		    ;; in case default-directory got munged.
		    (or (consp dired-directory)
			(null (file-directory-p dired-directory))))
      ;; The R-switch will clobber sorting of subdirs.
      ;; What is the right thing to do here?
      (dired-insert-old-subdirs old-subdir-alist))
    (dired-mark-remembered mark-alist)	; mark files that were marked
    (if kill-files-p (dired-do-hide dired-kill-marker-char))
    (run-hooks 'dired-after-readin-hook)	; no need to narrow
    ;; omit-expunge after the readin hook
    (save-excursion
      (mapcar (function (lambda (dir)
			  (if (dired-goto-subdir dir)
			      (dired-omit-expunge))))
	      omitted-subdirs))
    ;; hide subdirs that were hidden
    (save-excursion
      (mapcar (function (lambda (dir)
			  (if (dired-goto-subdir dir)
			      (dired-hide-subdir 1))))
	      hidden-subdirs))
    ;; Try to get back to where we were
    (or (and ofile (dired-goto-file ofile))
	(goto-char opoint))
    (dired-move-to-filename)
    (dired-update-mode-line t)
    (dired-update-mode-line-modified t)))

(defun dired-do-redisplay (&optional arg)
  "Redisplay all marked (or next ARG) files."
  (interactive "P")
  ;; message instead of making dired-map-over-marks show-progress is
  ;; much faster
  (dired-map-over-marks (let ((fname (dired-get-filename)))
			  (dired-uncache fname nil)
			  (message "Redisplaying %s..." fname)
			  (dired-update-file-line fname))
			arg)
  (dired-update-mode-line-modified t)
  (message "Redisplaying...done"))

(defun dired-redisplay-subdir (&optional arg)
  "Redisplay the current subdirectory.
With a prefix prompts for listing switches."
  (interactive "P")
  (let ((switches (and arg (dired-make-switches-list
			    (read-string "Switches for listing: "
					 (dired-make-switches-string
					  dired-internal-switches)))))
	(dir (dired-current-directory))
	(opoint (point))
	(ofile (dired-get-filename nil t)))
    (or switches
	(setq switches (nth 3 (assoc dir dired-subdir-alist))))
    (or switches
	(setq switches (delq ?R (copy-sequence dired-internal-switches))))
    (message "Redisplaying %s..." dir)
    (dired-uncache dir t)
    (dired-insert-subdir dir switches)
    (dired-update-mode-line-modified t)
    (or (and ofile (dired-goto-file ofile)) (goto-char opoint))
    (message "Redisplaying %s... done" dir)))

(defun dired-update-file-line (file)
  ;; Delete the current line, and insert an entry for FILE.
  ;; Does not update other dired buffers.  Use dired-relist-file for that.
  (let* ((start (save-excursion (skip-chars-backward "^\n\r") (point)))
	 (char (char-after start)))
    (dired-save-excursion
     ;; don't remember omit marks
     (if (memq char (list ?\040 dired-omit-marker-char))
	 (setq char nil))
     ;; Delete the current-line. Even though dired-add-entry will not
     ;; insert duplicates, the file for the current line may not be the same as
     ;; FILE. eg. dired-do-compress
     (delete-region (save-excursion (skip-chars-backward "^\n\r") (1- (point)))
		    (progn (skip-chars-forward "^\n\r") (point)))
     ;; dired-add-entry inserts at the end of the previous line.
     (forward-char 1)
     (dired-add-entry file char t))))

;;; Subroutines of dired-revert
;;; Some of these are also used when inserting subdirs.

;; Don't want to remember omit marks, in case omission regexps
;; were changed, before the dired-revert. If we don't unhide
;; omitted files, we won't see their marks. Therefore we use
;; dired-omit-unhide-region.

(defun dired-remember-marks (beg end)
  ;; Return alist of files and their marks, from BEG to END.
  (if selective-display			; must unhide to make this work.
      (let (buffer-read-only)
	(subst-char-in-region (point-min) (point-max) ?\r ?\n)
	(dired-do-hide dired-omit-marker-char)))
  (let (fil chr alist)
    (save-excursion
      (goto-char beg)
      (while (re-search-forward dired-re-mark end t)
	(if (setq fil (dired-get-filename nil t))
	    (setq chr (char-before)
		  alist (cons (cons fil chr) alist)))))
    alist))

(defun dired-mark-remembered (alist)
  ;; Mark all files remembered in ALIST.
  (let (elt fil chr)
    (while alist
      (setq elt (car alist)
	    alist (cdr alist)
	    fil (car elt)
	    chr (cdr elt))
      (if (dired-goto-file fil)
	  (save-excursion
	    (beginning-of-line)
	    (dired-substitute-marker (point) (char-after (point)) chr))))))

(defun dired-remember-hidden ()
  ;; Return a list of all hidden subdirs.
  (let ((l dired-subdir-alist) dir result min)
    (while l
      (setq dir (car (car l))
	    min (dired-get-subdir-min (car l))
	    l (cdr l))
      (if (and (>= min (point-min)) (<= min (point-max))
	       (dired-subdir-hidden-p dir))
	  (setq result (cons dir result))))
    result))

(defun dired-insert-old-subdirs (old-subdir-alist)
  ;; Try to insert all subdirs that were displayed before
  (let (elt dir switches)
    (while old-subdir-alist
      (setq elt (car old-subdir-alist)
	    old-subdir-alist (cdr old-subdir-alist)
	    dir (car elt)
	    switches (or (nth 3 elt) dired-internal-switches))
      (condition-case ()
	  (dired-insert-subdir dir switches)
	(error nil)))))

(defun dired-uncache (file dir-p)
  ;; Remove directory DIR from any directory cache.
  ;; If DIR-P is non-nil, then FILE is a directory
  (let ((handler (find-file-name-handler file 'dired-uncache)))
    (if handler
	(funcall handler 'dired-uncache file dir-p))))


;;;; -------------------------------------------------------------
;;;; Inserting subdirectories
;;;; -------------------------------------------------------------

(defun dired-maybe-insert-subdir (dirname &optional
					  switches no-error-if-not-dir-p)
  "Insert this subdirectory into the same dired buffer.
If it is already present, just move to it (type \\[dired-do-redisplay] to 
  refresh), else inserts it at its natural place (as ls -lR would have done).
With a prefix arg, you may edit the ls switches used for this listing.
  You can add `R' to the switches to expand the whole tree starting at
  this subdirectory.
This function takes some pains to conform to ls -lR output."
  (interactive
   (list (dired-get-filename)
	 (if current-prefix-arg
	     (dired-make-switches-list
	      (read-string "Switches for listing: "
			   (dired-make-switches-string
			    dired-internal-switches))))))
  (let ((opoint (point)))
    ;; We don't need a marker for opoint as the subdir is always
    ;; inserted *after* opoint.
    (setq dirname (file-name-as-directory dirname))
    (or (and (not switches)
	     (dired-goto-subdir dirname))
	(dired-insert-subdir dirname switches no-error-if-not-dir-p))
    ;; Push mark so that it's easy to find back.  Do this after the
    ;; insert message so that the user sees the `Mark set' message.
    (push-mark opoint)))

(defun dired-insert-subdir (dir-or-list &optional
					switches no-error-if-not-dir-p no-posn)
  "Insert this subdirectory into the same dired buffer.
If it is already present, overwrites previous entry,
  else inserts it at its natural place (as ls -lR would have done).
With a prefix arg, you may edit the ls switches used for this listing.
  You can add `R' to the switches to expand the whole tree starting at
  this subdirectory.
This function takes some pains to conform to ls -lR output."
  ;; NO-ERROR-IF-NOT-DIR-P needed for special filesystems like
  ;; Prospero where dired-ls does the right thing, but
  ;; file-directory-p has not been redefined.
  ;; SWITCHES should be a list.
  ;; If NO-POSN is non-nil, doesn't bother position the point at
  ;; the first nontrivial file line.  This can be used as an efficiency
  ;; hack when calling this from a program.
  (interactive
   (list (dired-get-filename)
	 (if current-prefix-arg
	     (dired-make-switches-list
	      (read-string "Switches for listing: "
			   (dired-make-switches-string
			    dired-internal-switches))))))
  (let ((dirname (if (consp dir-or-list) (car dir-or-list) dir-or-list)))
    (setq dirname (file-name-as-directory (expand-file-name dirname)))
    (or (dired-in-this-tree dirname (expand-file-name default-directory))
	(error  "%s: not in this directory tree" dirname))
    (or no-error-if-not-dir-p
	(file-directory-p dirname)
	(error  "Attempt to insert a non-directory: %s" dirname))
    (if switches
	(or (dired-compatible-switches-p dired-internal-switches switches)
	    (error "Cannot have subdirs with %s and %s switches together."
		   (dired-make-switches-string dired-internal-switches)
		   (dired-make-switches-string switches)))
      (setq switches dired-internal-switches))
    (let ((elt (assoc dirname dired-subdir-alist))
	  mark-alist opoint-max buffer-read-only)
      (if (memq ?R switches)
	  ;; avoid duplicated subdirs
	  (progn
	    (setq mark-alist (dired-kill-tree dirname t))
	    (dired-insert-subdir-newpos dirname))
	(if elt
	    ;; If subdir is already present, remove it and remember its marks
	    (setq mark-alist (dired-insert-subdir-del elt))
	  ;; else move to new position
	  (dired-insert-subdir-newpos dirname)))
      (setq opoint-max (point-max))
      (condition-case nil
	  (dired-insert-subdir-doupdate
	   dirname (dired-insert-subdir-doinsert dir-or-list switches)
	   switches elt mark-alist)
	(quit ; watch out for aborted inserts
	 (and (= opoint-max (point-max))
	      (null elt)
	      (equal (char-before) ?\n)
	      (delete-char -1))
	 (signal 'quit nil))))
    (or no-posn (dired-initial-position dirname))))

(defun dired-do-insert-subdir ()
  "Insert all marked subdirectories in situ that are not yet inserted.
Non-directories are silently ignored."
  (interactive)
  (let ((files (or (dired-get-marked-files)
		   (error "No files marked."))))
    (while files
      (if (file-directory-p (car files))
	  (save-excursion (dired-maybe-insert-subdir (car files))))
      (setq files (cdr files)))))

;;; Utilities for inserting subdirectories

(defun dired-insert-subdir-newpos (new-dir)
  ;; Find pos for new subdir, according to tree order.
  (let ((alist dired-subdir-alist) elt dir new-pos)
    (while alist
      (setq elt (car alist)
	    alist (cdr alist)
	    dir (car elt))
      (if (dired-tree-lessp dir new-dir)
	  ;; Insert NEW-DIR after DIR
	  (setq new-pos (dired-get-subdir-max elt)
		alist nil)))
    (goto-char new-pos))
  (insert "\n")
  (point))

(defun dired-insert-subdir-del (element)
  ;; Erase an already present subdir (given by ELEMENT) from buffer.
  ;; Move to that buffer position.  Return a mark-alist.
  (let ((begin-marker (dired-get-subdir-min element)))
    (goto-char begin-marker)
    ;; Are at beginning of subdir (and inside it!).  Now determine its end:
    (goto-char (dired-subdir-max))
    (prog1
	(dired-remember-marks begin-marker (point))
      (delete-region begin-marker (point)))))

(defun dired-insert-subdir-doinsert (dir-or-list switches)
  ;; Insert ls output after point and put point on the correct
  ;; position for the subdir alist.
  ;; Return the boundary of the inserted text (as list of BEG and END).
  ;; SWITCHES should be a non-nil list.
  (let ((begin (point))
	(dirname (if (consp dir-or-list) (car dir-or-list) dir-or-list))
	end)
    (message "Reading directory %s..." dirname)
    (if (string-equal dirname (car (car (reverse dired-subdir-alist))))
	;; top level directory may contain wildcards:
	(let ((dired-internal-switches switches))
	  (dired-readin-insert dired-directory
			       (null (file-directory-p dired-directory))))
      (let ((switches (dired-make-switches-string switches))
	    (insert-directory-program dired-ls-program))
	(if (consp dir-or-list)
	    (progn
	      (insert "list wildcard\n")
	      (mapcar
	       (function
		(lambda (x)
		  (insert-directory x switches t)))
	       (cdr dir-or-list)))
	  (insert-directory dirname switches nil t))))
    (message "Reading directory %s...done" dirname)
    (setq end (point-marker))
    (dired-indent-listing begin end)
    (dired-insert-set-properties begin end)
    ;;  call dired-insert-headerline afterwards, as under VMS dired-ls
    ;;  does insert the headerline itself and the insert function just
    ;;  moves point.
    ;;  Need a marker for END as this inserts text.
    (goto-char begin)
    (dired-insert-headerline dirname)
    ;; point is now like in dired-build-subdir-alist
    (prog1
	(list begin (marker-position end))
      (set-marker end nil))))

(defun dired-insert-subdir-doupdate (dirname beg-end switches elt mark-alist)
  ;; Point is at the correct subdir alist position for ELT,
  ;; BEG-END is the subdir-region (as list of begin and end).
  ;; SWITCHES must be a non-nil list.
  (if (memq ?R switches)
      ;; This will remove ?R from switches on purpose.
      (let ((dired-internal-switches (delq ?R switches)))
	(dired-build-subdir-alist))
    (if elt
	(progn
	  (set-marker (dired-get-subdir-min elt) (point-marker))
	  (setcar (nthcdr 3 elt) switches)
	  (if dired-verify-modtimes
	      (dired-set-file-modtime dirname dired-subdir-alist)))
      (dired-alist-add dirname (point-marker) dired-omit-files switches)))
  (save-excursion
    (let ((begin (nth 0 beg-end))
	  (end (nth 1 beg-end)))
      (goto-char begin)
      (save-restriction
	(narrow-to-region begin end)
	;; hook may add or delete lines, but the subdir boundary
	;; marker floats
	(run-hooks 'dired-after-readin-hook)
	(if mark-alist (dired-mark-remembered mark-alist))
	(dired-do-hide dired-kill-marker-char)
	(if (if elt (nth 2 elt) dired-omit-files)
	    (dired-omit-expunge nil t))))))


;;;; --------------------------------------------------------------
;;;; Dired motion commands -- moving around in the dired buffer.
;;;; --------------------------------------------------------------
	
(defun dired-next-line (arg)
  "Move down lines then position at filename.
Optional prefix ARG says how many lines to move; default is one line."
  (interactive "p")
  (next-line arg)
  (dired-move-to-filename)
  (dired-update-mode-line)
  (setq zmacs-region-stays t))

(defun dired-previous-line (arg)
  "Move up lines then position at filename.
Optional prefix ARG says how many lines to move; default is one line."
  (interactive "p")
  (previous-line arg)
  (dired-move-to-filename)
  (dired-update-mode-line)
  (setq zmacs-region-stays t))

(defun dired-scroll-up (arg)
  "Dired version of scroll up.
Scroll text of current window upward ARG lines; or near full screen if no ARG.
When calling from a program, supply a number as argument or nil."
  (interactive "P")
  (scroll-up arg)
  (dired-move-to-filename)
  (dired-update-mode-line)
  (setq zmacs-region-stays t))

(defun dired-scroll-down (arg)
  "Dired version of scroll-down.
Scroll text of current window down ARG lines; or near full screen if no ARG.
When calling from a program, supply a number as argument or nil."
  (interactive "P")
  (scroll-down arg)
  (dired-move-to-filename)
  (dired-update-mode-line)
  (setq zmacs-region-stays t))

(defun dired-beginning-of-buffer (arg)
  "Dired version of `beginning of buffer'."
  (interactive "P")
  (beginning-of-buffer arg)
  (dired-update-mode-line)
  (setq zmacs-region-stays t))

(defun dired-end-of-buffer (arg)
  "Dired version of `end-of-buffer'."
  (interactive "P")
  (end-of-buffer arg)
  (while (not (or (dired-move-to-filename) (dired-get-subdir) (bobp)))
    (forward-line -1))
  (dired-update-mode-line t)
  (setq zmacs-region-stays t))

(defun dired-next-dirline (arg &optional opoint)
  "Goto ARG'th next directory file line."
  (interactive "p")
  (if dired-re-dir
      (progn
	(dired-check-ls-l)
	(or opoint (setq opoint (point)))
	(if (if (> arg 0)
		(re-search-forward dired-re-dir nil t arg)
	      (beginning-of-line)
	      (re-search-backward dired-re-dir nil t (- arg)))
	    (progn
	      (dired-move-to-filename)		; user may type `i' or `f'
	      (dired-update-mode-line))
	  (goto-char opoint)
	  (error "No more subdirectories"))))
  (setq zmacs-region-stays t))

(defun dired-prev-dirline (arg)
  "Goto ARG'th previous directory file line."
  (interactive "p")
  (dired-next-dirline (- arg))
  (setq zmacs-region-stays t))

(defun dired-next-marked-file (arg &optional wrap opoint)
  "Move to the next marked file, wrapping around the end of the buffer."
  (interactive "p\np")
  (or opoint (setq opoint (point))) ; return to where interactively started
  (if (if (> arg 0)
	  (re-search-forward dired-re-mark nil t arg)
	(beginning-of-line)
	(re-search-backward dired-re-mark nil t (- arg)))
      (dired-move-to-filename)
    (if (null wrap)
	(progn
	  (goto-char opoint)
	  (error "No next marked file"))
      (message "(Wraparound for next marked file)")
      (goto-char (if (> arg 0) (point-min) (point-max)))
      (dired-next-marked-file arg nil opoint)))
  (dired-update-mode-line)
  (setq zmacs-region-stays t))

(defun dired-prev-marked-file (arg &optional wrap)
  "Move to the previous marked file, wrapping around the end of the buffer."
  (interactive "p\np")
  (dired-next-marked-file (- arg) wrap)
  (dired-update-mode-line)
  (setq zmacs-region-stays t))

(defun dired-goto-file (file)
  "Goto file line of FILE in this dired buffer."
  ;; Return value of point on success, else nil.
  ;; FILE must be an absolute pathname.
  ;; Loses if FILE contains control chars like "\007" for which ls
  ;; either inserts "?" or "\\007" into the buffer, so we won't find
  ;; it in the buffer.
  (interactive
   (prog1				; let push-mark display its message
       (list
	(let* ((dired-completer-buffer (current-buffer))
	       (dired-completer-switches dired-internal-switches)
	       (stack (reverse
		       (mapcar (function
				(lambda (x)
				  (dired-abbreviate-file-name (car x))))
			       dired-subdir-alist)))
	       (initial (car stack))
	       (dired-goto-file-history (cdr stack))
	       dired-completer-cache)
	  (expand-file-name
	   (dired-completing-read "Goto file: "
				  'dired-goto-file-completer
				  nil t initial 'dired-goto-file-history))))
     (push-mark)))
  (setq file (directory-file-name file)) ; does no harm if no directory
  (let (found case-fold-search)
    (save-excursion
      (if (dired-goto-subdir (or (file-name-directory file)
				 (error "Need absolute pathname for %s"
					file)))
	  (let* ((base (file-name-nondirectory file))
		 ;; filenames are preceded by SPC, this makes
		 ;; the search faster (e.g. for the filename "-"!).
		 (search (concat " " (dired-make-filename-string base t)))
		 (boundary (dired-subdir-max))
		 fn)
	    (while (and (not found) (search-forward search boundary 'move))
	      ;; Match could have BASE just as initial substring or
	      ;; or in permission bits or date or
	      ;; not be a proper filename at all:
	      (if (and (setq fn (dired-get-filename 'no-dir t))
		       (string-equal fn base))
		  ;; Must move to filename since an (actually
		  ;; correct) match could have been elsewhere on the
		  ;; line (e.g. "-" would match somewhere in the
		  ;; permission bits).
		  (setq found (dired-move-to-filename)))))))
    (and found
	 ;; return value of point (i.e., FOUND):
	 (prog1
	     (goto-char found)
	   (dired-update-mode-line)))))

;;; Moving by subdirectories

(defun dired-up-directory (arg)
  "Move to the ARG'th (prefix arg) parent directory of current directory.
Always stays within the current tree dired buffer.  Will insert new
subdirectories if necessary."
  (interactive "p")
  (if (< arg 0) (error "Can't go up a negative number of directories!"))
  (or (zerop arg)
      (let* ((dir (dired-current-directory))
	     (n arg)
	     (up dir))
	(while (> n 0)
	  (setq up (file-name-directory (directory-file-name up))
		n (1- n)))
	(if (and (< (length up) (length dired-directory))
		 (dired-in-this-tree dired-directory up))
	    (if (or (memq 'create-top-dir dired-no-confirm)
		    (y-or-n-p
		     (format "Insert new top dir %s and rename buffer? "
			     (dired-abbreviate-file-name up))))
		(let ((newname (let (buff)
				  (unwind-protect
				      (buffer-name
				       (setq buff
					     (create-file-buffer
					      (directory-file-name up))))
				    (kill-buffer buff))))
		      (buffer-read-only nil))
		  (push-mark)
		  (widen)
		  (goto-char (point-min))
		  (insert-before-markers "\n")
		  (forward-char -1)
		  (dired-insert-subdir-doupdate
		   up (dired-insert-subdir-doinsert up dired-internal-switches)
		   dired-internal-switches nil nil)
		  (dired-initial-position up)
		  (rename-buffer newname)
		  (dired-unadvertise default-directory)
		  (setq default-directory up
			dired-directory up)
		  (dired-advertise)))
	  (dired-maybe-insert-subdir up)))))

(defun dired-down-directory ()
  "Go down in the dired tree.
Moves to the first subdirectory of the current directory, which exists in
the dired buffer.  Does not take a prefix argument."
  ;; What would a prefix mean here?
  (interactive)
  (let ((dir (dired-current-directory)) ; has slash
	(rest (reverse dired-subdir-alist))
	pos elt)
    (while rest
      (setq elt (car rest))
      (if (dired-in-this-tree (directory-file-name (car elt)) dir)
	  (setq rest nil
		pos (dired-goto-subdir (car elt)))
	(setq rest (cdr rest))))
    (prog1
	(if pos
	    (progn
	      (push-mark)
	      (goto-char pos))
	  (error "At the bottom"))
      (dired-update-mode-line t))))

(defun dired-next-subdir (arg &optional no-error-if-not-found no-skip)
  "Go to next subdirectory, regardless of level."
  ;; Use 0 arg to go to this directory's header line.
  ;; NO-SKIP prevents moving to end of header line, returning whatever
  ;; position was found in dired-subdir-alist.
  (interactive "p")
  (let ((this-dir (dired-current-directory))
	pos index)
    ;; nth with negative arg does not return nil but the first element
    (setq index (- (length dired-subdir-alist)
		   (length (memq (assoc this-dir dired-subdir-alist)
				 dired-subdir-alist))
		   arg))
    (setq pos (if (>= index 0)
		  (dired-get-subdir-min (nth index dired-subdir-alist))))
    (if pos
	(if no-skip
	    (goto-char pos)
	  (goto-char pos)
	  (skip-chars-forward "^\r\n")
	  (if (equal (char-after (point)) ?\r)
	      (skip-chars-backward "." (- (point) 3)))
	  (dired-update-mode-line t)
	  (point))
      (if no-error-if-not-found
	  nil				; return nil if not found
	(error "%s directory" (if (> arg 0) "Last" "First"))))))

(defun dired-prev-subdir (arg &optional no-error-if-not-found no-skip)
  "Go to previous subdirectory, regardless of level.
When called interactively and not on a subdir line, go to this subdir's line."
  (interactive
   (list (if current-prefix-arg
	     (prefix-numeric-value current-prefix-arg)
	   ;; if on subdir start already, don't stay there!
	   (if (dired-get-subdir) 1 0))))
  (dired-next-subdir (- arg) no-error-if-not-found no-skip))

(defun dired-goto-subdir (dir)
  "Goto end of header line of DIR in this dired buffer.
Return value of point on success, otherwise return nil.
The next char is either \\n, or \\r if DIR is hidden."
  (interactive
   (prog1				; let push-mark display its message
       (list
	(let* ((table (mapcar
		       (function
			(lambda (x)
			  (list (dired-abbreviate-file-name
				 (car x)))))
		       dired-subdir-alist))
	       (stack (reverse (mapcar 'car table)))
	       (initial (car stack))
	       (dired-goto-file-history (cdr stack)))
	  (expand-file-name
	   (dired-completing-read "Goto subdirectory " table nil t
				  initial 'dired-goto-file-history))))
     (push-mark)))
  (setq dir (file-name-as-directory dir))
  (let ((elt (assoc dir dired-subdir-alist)))
    (and elt
	 ;; need to make sure that we get where we're going.
	 ;; beware: narrowing might be in effect
	 (= (goto-char (dired-get-subdir-min elt)) (point))
	 (progn
	   ;; dired-subdir-hidden-p and dired-add-entry depend on point being
	   ;; at either \n or looking-at ...\r after this function succeeds.
	   (skip-chars-forward "^\r\n")
	   (if (equal (char-before) ?.)
	       (skip-chars-backward "." (- (point) 3)))
	   (if (interactive-p) (dired-update-mode-line))
	   (point)))))

;;; Internals for motion commands

(defun dired-update-mode-line (&optional force)
  "Updates the mode line in dired according to the position of the point.
Normally this uses a cache of the boundaries of the current subdirectory,
but if the optional argument FORCE is non-nil, then modeline is always
updated and the cache is recomputed."
  (if (or force
	  (>= (point) dired-curr-subdir-max)
	  (< (point) dired-curr-subdir-min))
      (let ((alist dired-subdir-alist)
	    min max)
	(while (and alist (< (point)
			     (setq min (dired-get-subdir-min (car alist)))))
	  (setq alist (cdr alist)
		max min))
	(setq dired-curr-subdir-max (or max (point-max-marker))
	      dired-curr-subdir-min (or min (point-min-marker))
	      dired-subdir-omit (nth 2 (car alist)))
	(dired-sort-set-modeline (nth 3 (car alist))))))

(defun dired-manual-move-to-filename (&optional raise-error bol eol)
  "In dired, move to first char of filename on this line.
Returns position (point) or nil if no filename on this line."
  ;; This is the UNIX version.
  ;; have to be careful that we don't move to omitted files
  (let (case-fold-search)
    
    (or eol (setq eol (save-excursion (skip-chars-forward "^\r\n") (point))))
    (or bol (setq bol (progn (skip-chars-backward "^\r\n") (point))))
    
    (if (or (memq ?l dired-internal-switches)
	    (memq ?g dired-internal-switches))
	(if (and
	     (> (- eol bol) 17)         ; a valid file line must have at least
					; 17 chars. 2 leading, 10 perms,
					; separator, node #, separator, owner,
					; separator
	     (goto-char (+ bol 17))
	     (re-search-forward dired-re-month-and-time eol t))
	    (point)
	  (goto-char bol)
	  (if raise-error
	      (error "No file on this line")
	    nil))
      ;; else ls switches don't contain -l.
      ;; Note that even if we make dired-move-to-filename and
      ;; dired-move-to-end-of-filename (and thus dired-get-filename)
      ;; work, all commands that gleaned information from the permission
      ;; bits (like dired-mark-directories) will cease to work properly.
      (if (= bol eol)
	  (if raise-error
	      (error "No file on this line")
	    nil)
	;; skip marker, if any
	(goto-char bol)
	(forward-char))
      ;; If we not going to use the l switch, and use nstd listings,
      ;; then we must bomb on files starting with spaces.
      (skip-chars-forward " \t")
      (point))))

(defun dired-manual-move-to-end-of-filename (&optional no-error bol eol)
  ;; Assumes point is at beginning of filename,
  ;; thus the rwx bit re-search-backward below will succeed in *this*
  ;; line if at all.  So, it should be called only after
  ;; (dired-move-to-filename t).
  ;; On failure, signals an error (with non-nil NO-ERROR just returns nil).
  ;; This is the UNIX version.
  (let ((bof (point))
	file-type modes-start case-fold-search)
    (or eol (setq eol (save-excursion (skip-chars-forward "^\r\n") (point))))
    (or bol (setq bol (save-excursion (skip-chars-backward "^\r\n") (point))))
    (and
     (null no-error)
     selective-display
     (equal (char-after (1- bol)) ?\r)
     (cond
      ((dired-subdir-hidden-p (dired-current-directory))
       (error
	(substitute-command-keys
	 "File line is hidden. Type \\[dired-hide-subdir] to unhide.")))
      ((error
	(substitute-command-keys
	 "File line is omitted. Type \\[dired-omit-toggle] to un-omit.")))))
    (if (or (memq ?l dired-internal-switches)
	    (memq ?g dired-internal-switches))
	(if (save-excursion
	      (goto-char bol)
	      (re-search-forward
	       "[^ ][-r][-w][^ ][-r][-w][^ ][-r][-w][^ ][-+ 0-9+]"
	       bof t))
	    (progn
	      (setq modes-start (match-beginning 0)
		    file-type (char-after modes-start))
	      ;; Move point to end of name:
	      (if (equal file-type ?l) ; symlink
		  (progn
		    (if (search-forward " -> " eol t)
			(goto-char (match-beginning 0))
		      (goto-char eol))
		    (and dired-ls-F-marks-symlinks
			 (equal (char-before) ?@) ; link really marked?
			 (memq ?F dired-internal-switches)
			 (forward-char -1))
		    (point))
		;; else not a symbolic link
		(goto-char eol)
		;; ls -lF marks dirs, sockets and executables with exactly
		;; one trailing character. -F may not actually be honored, 
		;; e.g. by an FTP ls in efs
		(and
		 (memq ?F dired-internal-switches)
		 (let ((char (char-before)))
		   (or (and (equal char ?*) (or
					     (memq
					      (char-after (+ modes-start 3))
					      '(?x ?s ?t))
					     (memq
					      (char-after (+ modes-start 6))
					      '(?x ?s ?t))
					     (memq
					      (char-after (+ modes-start 9))
					      '(?x ?s ?t))))
		       (and (equal char ?=) (equal file-type ?s))))
		 (forward-char -1))
		;; Skip back over /'s unconditionally.  It's not a valid
		;; file name character.
		(skip-chars-backward "/")
		(point)))
	  (and (null no-error)
	       (error "No file on this line")))
      
      ;; A brief listing
      (if (eq (point) eol)
	  (and (null no-error)
	       (error "No file on this line"))
	(goto-char eol)
	(if (and (memq (char-before) '(?@ ?* ?=))
		 (memq ?F dired-internal-switches))
	    ;; A guess, since without a long listing, we can't be sure.
	    (forward-char -1))
	(skip-chars-backward "/")
	(point)))))

(defun dired-goto-next-nontrivial-file ()
  ;; Position point on first nontrivial file after point.
  ;; Does not move into the next sudir.
  ;; If point is on a file line, moves to that file.
  ;; This does not move to omitted files.
  (skip-chars-backward "^\n\r")
  (if (equal (char-before) ?\r)
      (forward-line 1))
  (let ((max (dired-subdir-max))
	file)
    (while (and (or (not (setq file (dired-get-filename 'no-dir t)))
		    (string-match dired-trivial-filenames file))
		(< (point) max))
      (forward-line 1)))
  (dired-move-to-filename))

(defun dired-goto-next-file ()
  ;; Doesn't move out of current subdir. Does go to omitted files.
  ;; Returns the starting position of the file, or nil if none found.
  (let ((max (dired-subdir-max))
	found)
    (while (and (null (setq found (dired-move-to-filename))) (< (point) max))
      (skip-chars-forward "^\n\r")
      (forward-char 1))
    found))

;; fluid vars used by dired-goto-file-completer
(defvar dired-completer-buffer nil)
(defvar dired-completer-switches nil)
(defvar dired-completer-cache nil)

(defun dired-goto-file-completer (string pred action)
  (save-excursion
    (set-buffer dired-completer-buffer)
    (let* ((saved-md (match-data))
	   (file (file-name-nondirectory string))
	   (dir (file-name-directory string))
	   (xstring (expand-file-name string))
	   (xdir (file-name-directory xstring))
	   (exact (dired-goto-file xstring)))
      (unwind-protect
	  (if (dired-goto-subdir xdir)
	      (let ((table (cdr (assoc xdir dired-completer-cache)))
		    fn result max)
		(or table
		    (progn
		      (setq table (make-vector 37 0))
		      (mapcar (function
			       (lambda (ent)
				 (setq ent (directory-file-name
					    (car ent)))
				 (if (string-equal
				      (file-name-directory ent) xdir)
				     (intern
				      (concat
				       (file-name-nondirectory ent) "/")
				      table))))
			      dired-subdir-alist)
		      (or (looking-at "\\.\\.\\.\n\r")
			  (progn
			    (setq max (dired-subdir-max))
			    (while (and
				    (< (point) max)
				    (not
				     (setq fn
					   (dired-get-filename 'no-dir t))))
			      (forward-line 1))
			    (if fn
				(progn
				  (or (intern-soft (concat fn "/") table)
				      (intern fn table))
				  (forward-line 1)
				  (while (setq fn
					       (dired-get-filename 'no-dir t))
				    (or (intern-soft (concat fn "/") table)
					(intern fn table))
				    (forward-line 1))))))
		      (setq dired-completer-cache (cons
						   (cons xdir table)
						   dired-completer-cache))))
		(cond
		 ((null action)
		  (setq result (try-completion file table))
		  (if exact
		      (if (stringp result)
			  string
			t)
		    (if (stringp result)
			(concat dir result)
		      result)))
		 ((eq action t)
		  (setq result (all-completions file table))
		  (if exact (cons "." result) result))
		 ((eq 'lambda action)
		  (and (or exact (intern-soft file table)))))))
	(store-match-data saved-md)))))

(defun dired-really-goto-file (file)
  ;; Goes to a file, even if it needs to insert it parent directory.
  (or (dired-goto-file file)
      (progn				; refresh and try again
	(dired-insert-subdir (file-name-directory file))
	(dired-goto-file file))))

(defun dired-between-files ()
  ;; Point must be at beginning of line
  (save-excursion (not (dired-move-to-filename nil (point)))))

(defun dired-repeat-over-lines (arg function)
  ;; This version skips non-file lines.
  ;; Skips file lines hidden with selective display.
  ;; BACKWARDS means move backwards after each action.  This is not the same
  ;; as a negative arg, as that skips the current line.
  (beginning-of-line)
  (let* ((advance (cond ((> arg 0) 1) ((< arg 0) -1) (t nil)))
	 (check-fun (if (eq advance 1) 'eobp 'bobp))
	 (n (if (< arg 0) (- arg) arg))
	 (wall (funcall check-fun))
	 (done wall))
    (while (not done)
      (if advance
	  (progn
	    (while  (not (or (save-excursion (dired-move-to-filename))
			     (setq wall (funcall check-fun))))
	      (forward-line advance))
	    (or wall
		(progn
		  (save-excursion (funcall function))
		  (forward-line advance)
		  (while (not (or (save-excursion (dired-move-to-filename))
				  (setq wall (funcall check-fun))))
		    (forward-line advance))
		  (setq done (or (zerop (setq n (1- n))) wall)))))
	(if (save-excursion (dired-move-to-filename))
	    (save-excursion (funcall function)))
	(setq done t))))
  (dired-move-to-filename)
  ;; Note that if possible the point has now been moved to the beginning of
  ;; the file name.
  (dired-update-mode-line))


;;;; ----------------------------------------------------------------
;;;; Miscellaneous dired commands
;;;; ----------------------------------------------------------------

(defun dired-quit ()
  "Bury the current dired buffer."
  (interactive)
  (bury-buffer))

(defun dired-undo ()
  "Undo in a dired buffer.
This doesn't recover lost files, it is just normal undo with temporarily
writeable buffer.  You can use it to recover marks, killed lines or subdirs."
  (interactive)
  (let ((lines (count-lines (point-min) (point-max)))
	buffer-read-only)
    (undo)
    ;; reset dired-subdir-alist, if a dir may have been affected
    ;; Is there a better way to guess this?
    (setq lines (- (count-lines (point-min) (point-max)) lines))
    (if (or (>= lines 2) (<= lines -2))
	(dired-build-subdir-alist)))
  (dired-update-mode-line-modified t)
  (dired-update-mode-line t))


;;;; --------------------------------------------------------
;;;; Immediate actions on files: visiting, viewing, etc.
;;;; --------------------------------------------------------

(defun dired-find-file ()
  "In dired, visit the file or directory named on this line."
  (interactive)
  (let ((find-file-run-dired t))
    (find-file (dired-get-filename))))

(defun dired-find-and-bury-buffer ()
  "*In dired, visit the file or directory on this line and bury it."
  (interactive)
  (let ((find-file-run-dired t))
    (find-file (dired-get-filename))
    (bury-buffer)))


(defun dired-view-file ()
  "In dired, examine a file in view mode, returning to dired when done.
When file is a directory, show it in this buffer if it is inserted;
otherwise, display it in another buffer."
  (interactive)
  (let ((file (dired-get-filename)))
    (if (file-directory-p file)
	(or (dired-goto-subdir file)
	    (dired file))
      (view-file file))))

(defun dired-find-file-other-window (&optional displayp)
  "In dired, visit this file or directory in another window.
With a prefix, the file is displayed, but the window is not selected."
  (interactive "P")
  (if displayp
      (dired-display-file)
      (let ((find-file-run-dired t))
        (find-file-other-window (dired-get-filename)))))

;; Only for Emacs 19
(defun dired-find-file-other-frame ()
  "In dired, visit this file or directory in another frame."
  (interactive)
  (let ((find-file-run-dired t))
    (find-file-other-frame (dired-get-filename))))

(defun dired-display-file ()
  "In dired, displays this file or directory in the other window."
  (interactive)
  (let ((find-file-run-dired t))
    (display-buffer (find-file-noselect (dired-get-filename)))))

(defun dired-find-alternate-file ()
  "In dired, replace the current buffer with the file or directory named on this line."
    (interactive)
    (find-alternate-file (dired-get-filename)))

(defun dired-do-find-marked-files (&optional arg)
  "find the marked (or next ARG) files into xemacs."
  (interactive "P")
  (dired-map-over-marks-check (function dired-find-and-bury-buffer) arg 'load "load" t))

;; After an idea by wurgler@zippysun.math.uakron.edu (Tom Wurgler).
(defun dired-do-find-file (&optional arg)
  "Visit all marked files at once, and display them simultaneously.
See also function `simultaneous-find-file'.
If you want to keep the dired buffer displayed, type \\[split-window-vertically] first.
If you want just the marked files displayed and nothing else, type \\[delete-other-windows] first."
  (interactive "P")
  (dired-simultaneous-find-file (dired-get-marked-files nil arg)))

(defun dired-simultaneous-find-file (file-list)
  "Visit all files in FILE-LIST and display them simultaneously.

The current window is split across all files in FILE-LIST, as evenly
as possible.  Remaining lines go to the bottommost window.

The number of files that can be displayed this way is restricted by
the height of the current window and the variable `window-min-height'."
  ;; It is usually too clumsy to specify FILE-LIST interactively
  ;; unless via dired (dired-do-find-file).
  (let ((size (/ (window-height) (length file-list))))
    (or (<= window-min-height size)
	(error "Too many files to visit simultaneously"))
    (find-file (car file-list))
    (setq file-list (cdr file-list))
    (while file-list
      ;; Split off vertically a window of the desired size
      ;; The upper window will have SIZE lines.  We select the lower
      ;; (larger) window because we want to split that again.
      (select-window (split-window nil size))
      (let ((find-file-run-dired t))
        (find-file (car file-list)))
      (setq file-list (cdr file-list)))))

(defun dired-create-directory (directory)
  "Create a directory called DIRECTORY."
  (interactive
   (list (read-file-name "Create directory: "
			 (dired-abbreviate-file-name
			  (dired-current-directory)))))
  (let ((expanded (expand-file-name directory)))
    (make-directory expanded)
    ;; Because this function is meant to be called interactively, it moves
    ;; the point.
    (dired-goto-file expanded)))

(defun dired-recover-file ()
  "Recovers file from its autosave file.
If the file is an autosave file, then recovers its associated file instead."
  (interactive)
  (let* ((file (dired-get-filename))
	 (name (file-name-nondirectory file))
	 (asp (auto-save-file-name-p name))
	 (orig (and
		asp
		(if (fboundp 'auto-save-original-name)
		    (auto-save-original-name file)
		  (error
		   "Need auto-save package to compute original file name."))))
	 (buff (if asp
		   (and orig (get-file-buffer orig))
		 (get-file-buffer file))))
    (and
     buff
     (buffer-modified-p buff)
     (or
      (yes-or-no-p
       (format
	"Recover file will erase the modified buffer %s.  Do it? "
	(buffer-name buff)))
      (error "Recover file aborted.")))
    (if asp
	(if orig
	    (recover-file orig)
	  (find-file file))
      (recover-file file))))


;;;; --------------------------------------------------------------------
;;;; Functions for extracting and manipulating file names
;;;; --------------------------------------------------------------------

(defun dired-make-filename-string (filename &optional reverse)
  ;; Translates the way that a file name appears in a buffer, to
  ;; how it is used in a path name.  This is useful for non-unix
  ;; support in efs.
  filename)

(defun dired-get-filename (&optional localp no-error-if-not-filep)
  "In dired, return name of file mentioned on this line.
Value returned normally includes the directory name.
Optional arg LOCALP with value `no-dir' means don't include directory
  name in result.  A value of t means use path name relative to
  `default-directory', which still may contain slashes if in a subdirectory.
Optional arg NO-ERROR-IF-NOT-FILEP means return nil if no filename on
  this line, otherwise an error occurs."

  ;; Compute bol & eol once, rather than twice inside move-to-filename
  ;; and move-to-end-of-filename
  (let ((eol (save-excursion (skip-chars-forward "^\n\r") (point)))
	(bol (save-excursion (skip-chars-backward "^\r\n") (point)))
	case-fold-search file p1 p2)
    (save-excursion
      (and
       (setq p1 (dired-move-to-filename (not no-error-if-not-filep) bol eol))
       (setq p2 (dired-move-to-end-of-filename no-error-if-not-filep bol eol))
       (setq file (buffer-substring p1 p2))
       ;; Check if ls quoted the names, and unquote them.
       ;; Using read to unquote is much faster than substituting
       ;; \007 (4 chars) -> ^G  (1 char) etc. in a lisp loop.
       (cond ((memq ?b dired-internal-switches) ; System V ls
	      ;; This case is about 20% slower than without -b.
	      (setq file
		    (read
		     (concat "\""
			       ;; some ls -b don't escape quotes, argh!
			     ;; This is not needed for GNU ls, though.
			     (or (dired-string-replace-match
				    "\\([^\\]\\)\"" file "\\1\\\\\"")
				 file)
			     "\""))))
	       ;; If you do this, update dired-compatible-switches-p
	     ;; ((memq ?Q dired-internal-switches) ; GNU ls
	     ;;  (setq file (read file)))
	       )))
    (and file
	 (if (eq localp 'no-dir)
	     (dired-make-filename-string file)
	   (concat (dired-current-directory localp)
		   (dired-make-filename-string file))))))

(defun dired-make-relative (file &optional dir no-error)
  ;; Convert FILE (an *absolute* pathname) to a pathname relative to DIR.
  ;; FILE must be absolute, or this function will return nonsense.
  ;; If FILE is not in a subdir of DIR, an error is signalled,
  ;; unless NO-ERROR is t. Then, ".."'s are inserted to give
  ;; a relative representation of FILE wrto DIR
  ;; eg.     FILE = /vol/tex/bin/foo DIR = /vol/local/bin/
  ;;         results in  ../../tex/bin/foo
  ;; DIR must be expanded.
  ;; DIR defaults to default-directory.
  ;; DIR must be file-name-as-directory, as with all directory args in
  ;; elisp code.
  (or dir (setq dir (expand-file-name default-directory)))
  (let ((flen (length file))
	(dlen (length dir)))
    (if (and (> flen dlen)
	     (string-equal (substring file 0 dlen) dir))
	(substring file dlen)
      ;; Need to insert ..'s
      (or no-error (error  "%s: not in directory tree growing at %s" file dir))
      (if (string-equal file dir)
	  "./"
	(let ((index 1)
	      (count 0))
	  (while (and (string-match "/" dir index)
		      (<= (match-end 0) flen)
		      (string-equal (substring file index (match-end 0))
				    (substring dir index (match-end 0))))
	    (setq index (match-end 0)))
	  (setq file (substring file index))
	  (if (and (/= flen index)
		   (not (string-match "/" file))
		   (< flen dlen)
		   (string-equal file (substring dir index flen))
		   (= (aref dir flen) ?/))
	      (setq file "."
		    count -1))
	  ;; count how many slashes remain in dir.
	  (while (string-match "/" dir index)
	    (setq index (match-end 0)
		  count (1+ count)))
	  (apply 'concat (nconc (make-list count "../") (list file))))))))

;;; Functions for manipulating file names.
;;
;;  Used by file tranformers.
;;  Define here rather than in dired-shell.el, as it wouldn't be
;;  unreasonable to use these elsewhere.

(defun dired-file-name-base (fn)
  "Returns the base name of FN.
This is the file without directory part, and extension. See the variable
`dired-filename-re-ext'."
  (setq fn (file-name-nondirectory fn))
  (if (string-match dired-filename-re-ext fn 1)
      (substring fn 0 (match-beginning 0))
    fn))

(defun dired-file-name-extension (fn)
  "Returns the extension for file name FN.
See the variable dired-filename-re-ext'."
  (setq fn (file-name-nondirectory fn))
  (if (string-match dired-filename-re-ext fn 1)
      (substring fn (match-beginning 0))
    ""))

(defun dired-file-name-sans-rcs-extension (fn)
  "Returns the file name FN without its RCS extension \",v\"."
  (setq fn (file-name-nondirectory fn))
  (if (string-match ",v\\'" fn 1)
      (substring fn 0 (match-beginning 0))
    fn))

(defun dired-file-name-sans-compress-extension (fn)
  "Returns the file name FN without the extension from compress or gzip."
  (setq fn (file-name-nondirectory fn))
  (if (string-match "\\.\\([zZ]\\|gz\\)\\'" fn 1)
      (substring fn (match-beginning 0))
    fn))


;;;; ---------------------------------------------------------------------
;;;; Working with directory trees.
;;;; ---------------------------------------------------------------------
;;;
;;;  This where code for the dired-subdir-alist is.

;;; Utility functions for dired-subdir-alist

(defun dired-normalize-subdir (dir)
  ;; Prepend default-directory to DIR if relative path name.
  ;; dired-get-filename must be able to make a valid filename from a
  ;; file and its directory DIR.
  ;; Fully expand everything.
  (file-name-as-directory
   (if (file-name-absolute-p dir)
       (expand-file-name dir)
     (expand-file-name dir (expand-file-name default-directory)))))

(defun dired-get-subdir ()
  ;;"Return the subdir name on this line, or nil if not on a headerline."
  ;; Look up in the alist whether this is a headerline.
  (save-excursion
    (let ((cur-dir (dired-current-directory)))
      (beginning-of-line)		; alist stores b-o-l positions
      (and (zerop (- (point)
		     (dired-get-subdir-min (assoc cur-dir
						  dired-subdir-alist))))
	   cur-dir))))

(defun dired-get-subdir-max (elt)
  ;; returns subdir max.
  (let ((pos (- (length dired-subdir-alist)
		(length (member elt dired-subdir-alist)))))
    (if (zerop pos)
	(point-max)
      (1- (dired-get-subdir-min (nth (1- pos) dired-subdir-alist))))))

(defun dired-clear-alist ()
  ;; Set all markers in dired-subdir-alist to nil.  Set the alist to nil too.
  (while dired-subdir-alist
    (set-marker (dired-get-subdir-min (car dired-subdir-alist)) nil)
    (setq dired-subdir-alist (cdr dired-subdir-alist))))

(defun dired-unsubdir (dir)
  ;; Remove DIR from the alist
  (setq dired-subdir-alist
	(delq (assoc dir dired-subdir-alist) dired-subdir-alist)))

(defun dired-simple-subdir-alist ()
  ;; Build and return `dired-subdir-alist' assuming just the top level
  ;; directory to be inserted.  Don't parse the buffer.
  (setq dired-subdir-alist
	(list (list (expand-file-name default-directory)
		    (point-min-marker) dired-omit-files
		    dired-internal-switches nil)))
  (if dired-verify-modtimes
      (dired-set-file-modtime (expand-file-name default-directory)
			      dired-subdir-alist)))

(defun dired-build-subdir-alist ()
  "Build `dired-subdir-alist' by parsing the buffer and return its new value."
  (interactive)
  (let ((o-alist dired-subdir-alist)
	(count 0)
	subdir)
    (dired-clear-alist)
    (save-excursion
      (goto-char (point-min))
      (while (re-search-forward dired-subdir-regexp nil t)
	(setq count (1+ count))
	(apply 'dired-alist-add-1
	       (setq subdir (buffer-substring (match-beginning 2)
					      (match-end 2)))
	       ;; Put subdir boundary between lines.
	       (set-marker (make-marker) (match-end 1))
	       (let ((elt (assoc subdir o-alist)))
		 (if elt
		     (list (nth 2 elt) (nth 3 elt))
		   (list dired-omit-files dired-internal-switches)))))
      (if (interactive-p)
	  (message "%d director%s." count (if (= 1 count) "y" "ies")))
      ;; We don't need to sort it because it is in buffer order per
      ;; constructionem.  Return new alist:
      ;; pointers for current-subdir may be stale
      dired-subdir-alist)))

(defun dired-alist-add (dir new-marker &optional omit switches)
  ;; Add new DIR at NEW-MARKER.  Sort alist.
  (dired-alist-add-1 dir new-marker omit switches)
  (dired-alist-sort))

(defun dired-alist-add-1 (dir new-marker &optional omit switches)
  ;; Add new DIR at NEW-MARKER.  Don't sort.
  (let ((dir (dired-normalize-subdir dir)))
    (setq dired-subdir-alist
	  (cons (list dir new-marker omit switches nil) dired-subdir-alist))
    (if dired-verify-modtimes
	(dired-set-file-modtime dir dired-subdir-alist))))

(defun dired-alist-sort ()
  ;; Keep the alist sorted on buffer position.
  (setq dired-subdir-alist
	(sort dired-subdir-alist
	      (function (lambda (elt1 elt2)
			  (> (dired-get-subdir-min elt1)
			     (dired-get-subdir-min elt2)))))))

;;; Utilities for working with subdirs in the dired buffer

;; This function is the heart of tree dired.
;; It is called for each retrieved filename.
;; It could stand to be faster, though it's mostly function call
;; overhead.  Avoiding to funcall seems to save about 10% in
;; dired-get-filename.  Make it a defsubst?
(defun dired-current-directory (&optional localp)
  "Return the name of the subdirectory to which this line belongs.
This returns a string with trailing slash, like `default-directory'.
Optional argument means return a file name relative to `default-directory'.
In this it returns \"\" for the top directory."
  (let* ((here (point))
	 (dir (catch 'done
		(mapcar (function
			 (lambda (x)
			   (if (<= (dired-get-subdir-min x) here)
			       (throw 'done (car x)))))
			dired-subdir-alist))))
    (if (listp dir) (error "dired-subdir-alist seems to be mangled"))
    (if localp
	(let ((def-dir (expand-file-name default-directory)))
	  (if (string-equal dir def-dir)
	      ""
	    (dired-make-relative dir def-dir)))
      dir)))

;; Subdirs start at the beginning of their header lines and end just
;; before the beginning of the next header line (or end of buffer).

(defun dired-subdir-min ()
  ;; Returns the minimum position of the current subdir
  (save-excursion
    (if (not (dired-prev-subdir 0 t t))
	(error "Not in a subdir!")
      (point))))

(defun dired-subdir-max ()
  ;; Returns the maximum position of the current subdir
  (save-excursion
    (if (dired-next-subdir 1 t t)
	(1- (point)) ; Do not include separating empty line.
      (point-max))))


;;;; --------------------------------------------------------
;;;; Deleting files
;;;; --------------------------------------------------------

(defun dired-flag-file-deletion (arg)
  "In dired, flag the current line's file for deletion.
With prefix arg, repeat over several lines.

If on a subdir headerline, mark all its files except `.' and `..'."
  (interactive "p")
  (dired-mark arg dired-del-marker))

(defun dired-flag-file-deletion-backup (arg)
  "Flag current file for deletion, and move to previous line.
With a prefix ARG, repeats this ARG times."
  (interactive "p")
  ;; Use dired-mark-file and not dired-mark, as this function
  ;; should do nothing special on subdir headers.
  (dired-mark-file (- arg) dired-del-marker))

(defun dired-flag-subdir-files ()
  "Flag all the files in the current subdirectory for deletion."
  (interactive)
  (dired-mark-subdir-files dired-del-marker))

(defun dired-unflag (arg)
  "In dired, remove a deletion flag from the current line's file.
Optional prefix ARG says how many lines to unflag."
  (interactive "p")
  (let (buffer-read-only)
    (dired-repeat-over-lines
     arg
     (function
      (lambda ()
	(if (char-equal (char-after (point)) dired-del-marker)
	    (progn
	      (setq dired-del-flags-number (max (1- dired-del-flags-number) 0))
	      (dired-substitute-marker (point) dired-del-marker ?\ )))))))
  (dired-update-mode-line-modified))

(defun dired-backup-unflag (arg)
  "In dired, move up lines and remove deletion flag there.
Optional prefix ARG says how many lines to unflag; default is one line."
  (interactive "p")
  (dired-unflag (- arg)))

(defun dired-update-marker-counters (char &optional remove)
  (or (memq char '(?\  ?\n ?\r))
      (let ((counter (cond
		      ((char-equal char dired-del-marker)
		       'dired-del-flags-number)
		      ((char-equal char dired-marker-char)
		       'dired-marks-number)
		  ('dired-other-marks-number))))
	(if remove
	    (set counter (max (1- (symbol-value counter)) 0))
	  (set counter (1+ (symbol-value counter)))))))

(defun dired-update-mode-line-modified (&optional check)
  ;; Updates the value of mode-line-modified in dired.
  ;; Currently assumes that it's of the form "-%%-", where % sometimes
  ;; gets replaced by %.  Should allow some sort of config flag.
  ;; SET is t to set to -DD-, nil to set to -%%-, and 'check means
  ;; examine the buffer to find out.
  (if check
      (save-excursion
	(let (char)
	  (goto-char (point-min))
	  (setq dired-del-flags-number 0
		dired-marks-number 0
		dired-other-marks-number 0)
	  (while (not (eobp))
	    (setq char (char-after (point)))
	    (cond
	     ((char-equal char dired-del-marker)
	      (setq dired-del-flags-number (1+ dired-del-flags-number)))
	     ((char-equal char dired-marker-char)
	      (setq dired-marks-number (1+ dired-marks-number)))
	     ((memq char '(?\  ?\n ?\r))
	      nil)
	     ((setq dired-other-marks-number (1+ dired-other-marks-number))))
	    (forward-line 1)))))
  (setq mode-line-modified
	(format dired-mode-line-modified
		(if (zerop dired-del-flags-number)
		    "--"
		  (format "%d%c" dired-del-flags-number dired-del-marker))
		(if (zerop dired-marks-number)
		    "--"
		  (format "%d%c" dired-marks-number dired-marker-char))
		(if (zerop dired-other-marks-number)
		    "-"
		  (int-to-string dired-other-marks-number))))
  (set-buffer-modified-p (buffer-modified-p)))

(defun dired-do-deletions (&optional nomessage)
  (dired-expunge-deletions))

(defun dired-expunge-deletions ()
  "In dired, delete the files flagged for deletion."
  (interactive)
  (let ((files (let ((dired-marker-char dired-del-marker))
		 (dired-map-over-marks (cons (dired-get-filename) (point))
				       t))))
    (if files
	(progn
	  (dired-internal-do-deletions files nil dired-del-marker)
	  ;; In case the point gets left somewhere strange -- hope that
	  ;; this doesn't cause asynch troubles later.
	  (beginning-of-line)
	  (dired-goto-next-nontrivial-file)
	  (dired-update-mode-line-modified t)) ; play safe, it's cheap
      (message "(No deletions requested)"))))

(defun dired-do-delete (&optional arg)
  "Delete all marked (or next ARG) files."
  ;; This is more consistent with the file marking feature than
  ;; dired-expunge-deletions.
  (interactive "P")
  (dired-internal-do-deletions
   ;; this may move point if ARG is an integer
   (dired-map-over-marks (cons (dired-get-filename) (point))
		   arg)
   arg)
  (beginning-of-line)
  (dired-goto-next-nontrivial-file))

(defun dired-internal-do-deletions (l arg &optional marker-char)
  ;; L is an alist of files to delete, with their buffer positions.
  ;; ARG is the prefix arg.
  ;; Filenames are absolute (VMS needs this for logical search paths).
  ;; (car L) *must* be the *last* (bottommost) file in the dired buffer.
  ;; That way as changes are made in the buffer they do not shift the
  ;; lines still to be changed, so the (point) values in L stay valid.
  ;; Also, for subdirs in natural order, a subdir's files are deleted
  ;; before the subdir itself - the other way around would not work.
  (save-excursion
    (let ((files (mapcar (function car) l))
	  (count (length l))
	  (succ 0)
	  (cdir (dired-current-directory))
	  failures)
      ;; canonicalize file list for pop up
      (setq files (nreverse (mapcar (function
				     (lambda (fn)
				       (dired-make-relative fn cdir t)))
				    files)))
      (if (or (memq 'delete dired-no-confirm)
	      (dired-mark-pop-up
	       " *Files Flagged for Deletion*" 'delete files
	       dired-deletion-confirmer
	       (format "Delete %s "
		       (dired-mark-prompt arg files marker-char))))
	  (save-excursion
	    ;; files better be in reverse order for this loop!
	    (while l
	      (goto-char (cdr (car l)))
	      (condition-case err
		  (let ((fn (car (car l))))
		    ;; This test is equivalent to
		    ;; (and (file-directory-p fn)
		    ;;      (not (file-symlink-p fn)))
		    ;; but more efficient
		    (if (if (eq t (car (file-attributes fn)))
			    (if (<= (length (directory-files fn)) 2)
				(progn (delete-directory fn) t)
			      (and (or
				    (memq 'recursive-delete dired-no-confirm)
				    (funcall
				     dired-deletion-confirmer
				     (format "\
Recursively delete directory and files within %s? "
					     (dired-make-relative fn))))
				   (progn
				     (dired-recursive-delete-directory fn)
				     t)))
			  (progn (delete-file fn) t))
			(progn
			  (setq succ (1+ succ))
			  (message "%s of %s deletions" succ count)
			  (dired-clean-up-after-deletion fn))))
		(error;; catch errors from failed deletions
		 (dired-log (buffer-name (current-buffer)) "%s\n" err)
		 (setq failures (cons (car (car l)) failures))))
	      (setq l (cdr l)))))
      (if failures
	  (dired-log-summary
	   (buffer-name (current-buffer))
	   (format "%d of %d deletion%s failed:" (length failures) count
		   (dired-plural-s count))
	   failures)
	(if (zerop succ)
	    (message "(No deletions performed)")
	  (message "%d deletion%s done" succ (dired-plural-s succ)))))))

(defun dired-recursive-delete-directory (fn)
  ;; Recursively deletes directory FN, and all of its contents.
  (let* ((fn (expand-file-name fn))
	 (handler (find-file-name-handler
		   fn 'dired-recursive-delete-directory)))
    (if handler
	(funcall handler 'dired-recursive-delete-directory fn)
      (progn
	(or (file-exists-p fn)
	    (signal
	     'file-error
	     (list "Removing old file name" "no such directory" fn)))
	(let ((files (directory-files fn t)))
	  (while files
	    (let ((file (car files)))
	      (if (not (member (file-name-nondirectory file)
			       '("." "..")))
		  ;; This test is equivalent to
		  ;; (and (file-directory-p fn)
		  ;;      (not (file-symlink-p fn)))
		  ;; but more efficient
		  (if (eq t (car (file-attributes file)))
		      (dired-recursive-delete-directory file)
		    (delete-file file)))
	      (setq files (cdr files))))
	  (delete-directory fn))))))

(defun dired-clean-up-after-deletion (fn)
  ;; Offer to kill buffer of deleted file FN.
  (let ((buf (get-file-buffer fn)))
    (and buf
	 (or (memq 'kill-file-buffer dired-no-confirm)
	     (funcall (function yes-or-no-p)
		      (format "Kill buffer of %s, too? "
			      (file-name-nondirectory fn))))
	 (save-excursion ; you never know where kill-buffer leaves you
	   (kill-buffer buf)))))

;;; Cleaning a directory -- flagging backups for deletion

(defun dired-clean-directory (keep &optional marker msg)
  "Flag numerical backups for deletion.
Spares `dired-kept-versions' latest versions, and `kept-old-versions' oldest.
Positive prefix arg KEEP overrides `dired-kept-versions';
Negative prefix arg KEEP overrides `kept-old-versions' with KEEP made positive.

To clear the flags on these files, you can use \\[dired-flag-backup-files]
with a prefix argument."
  (interactive "P")
  (setq keep (if keep (prefix-numeric-value keep) dired-kept-versions))
  (let* ((early-retention (if (< keep 0) (- keep) kept-old-versions))
	 (late-retention (if (<= keep 0) dired-kept-versions keep))
	 (msg (or msg
		  (format
		   "Cleaning numerical backups (keeping %d late, %d old)"
		   late-retention early-retention)))
	 (trample-marker (or marker dired-del-marker))
	 (file-version-assoc-list))
    (message "%s..." msg)
    ;; Do this after messaging, as it may take a while.
    (setq file-version-assoc-list (dired-collect-file-versions))
    ;; Sort each VERSION-NUMBER-LIST,
    ;; and remove the versions to be deleted.
    (let ((fval file-version-assoc-list))
      (while fval
	(let* ((sorted-v-list (cons 'q (sort (cdr (car fval)) '<)))
	       (v-count (length sorted-v-list)))
	  (if (> v-count (+ early-retention late-retention))
	      (rplacd (nthcdr early-retention sorted-v-list)
		      (nthcdr (- v-count late-retention)
			      sorted-v-list)))
	  (rplacd (car fval)
		  (cdr sorted-v-list)))
	(setq fval (cdr fval))))
    ;; Look at each file.  If it is a numeric backup file,
    ;; find it in a VERSION-NUMBER-LIST and maybe flag it for deletion.
    (dired-map-dired-file-lines (function
				 (lambda (fn)
				   (dired-trample-file-versions
				    fn file-version-assoc-list
				    trample-marker))))
    (message "%s...done" msg)))

(defun dired-collect-file-versions ()
  ;; If it looks like a file has versions, return a list of the versions.
  ;; The return value is ((FILENAME . (VERSION1 VERSION2 ...)) ...)
  (let (result)
    (dired-map-dired-file-lines
     (function
      (lambda (fn)
	(let* ((base-versions
		(concat (file-name-nondirectory fn) ".~"))
	       (bv-length (length base-versions))
	       (possibilities (file-name-all-completions
			       base-versions
			       (file-name-directory fn))))
	  (if possibilities
	      (setq result (cons (cons fn
				       (mapcar 'backup-extract-version
					       possibilities)) result)))))))
    result))

(defun dired-trample-file-versions (fn alist marker)
  ;; ALIST is an alist of filenames and versions used to determine
  ;; if each file should be flagged for deletion.
  ;; This version using file-name-sans-versions is probably a lot slower
  ;; than Sebastian's original, but it is more easily adaptable to non-unix.
  (let ((base (file-name-sans-versions fn))
	base-version-list bv-length)
    (and (not (string-equal base fn))
	 (setq base-version-list (assoc base alist))
	 (setq bv-length (string-match "[0-9]" fn (length base)))
	 (not (memq (backup-extract-version fn) base-version-list))
	 (progn (skip-chars-backward "^\n\r")
		(bolp)) ; make sure the preceding char isn't \r.
	 (dired-substitute-marker (point) (char-after (point)) marker))))

(defun dired-map-dired-file-lines (fun)
  ;; Perform FUN with point at the end of each non-directory line.
  ;; FUN takes one argument, the filename (complete pathname).
  (dired-check-ls-l)
  (save-excursion
    (let (file buffer-read-only)
      (goto-char (point-min))
      (while (not (eobp))
	(save-excursion
	  (and (not (and dired-re-dir (looking-at dired-re-dir)))
	       (not (memq (char-after (point)) '(?\n ?\n)))
	       (setq file (dired-get-filename nil t)) ; nil on non-file
	       (progn (skip-chars-forward "^\n\r")
		      (funcall fun file))))
	(forward-line 1)))))            ; this guarantees that we don't
					; operate on omitted files.


;;;; -----------------------------------------------------------
;;;; Confirmations and prompting the user.
;;;; -----------------------------------------------------------

(defun dired-plural-s (count)
  (if (= 1 count) "" "s"))

(defun dired-mark-prompt (arg files &optional marker-char)
  ;; Return a string for use in a prompt, either the current file
  ;; name, or the marker and a count of marked files.
  (let ((count (length files)))
    (if (= count 1)
	(car files)
      ;; more than 1 file:
      (if (integerp arg)
	  (cond ((zerop arg) "[no files]")
		((> arg 0) "[following]")
		((< arg 0) "[preceding]"))
	(char-to-string (or marker-char dired-marker-char))))))

(defun dired-pop-to-buffer (buf)
  ;; Pop up buffer BUF.
  ;; Make its window fit its contents.
  (if temp-buffer-show-function
      (funcall temp-buffer-show-function buf)
    (let ((window (selected-window))
	  target-lines w2)
      (cond;; if split-window-threshold is enabled, use the largest window
       ((and (> (window-height (setq w2 (get-largest-window)))
		split-height-threshold)
	     (= (frame-width) (window-width w2)))
	(setq window w2))
       ;; if the least-recently-used window is big enough, use it
       ((and (> (window-height (setq w2 (get-lru-window)))
		(* 2 window-min-height))
	     (= (frame-width) (window-width w2)))
	(setq window w2)))
      (save-excursion
	(set-buffer buf)
	(goto-char (point-max))
	(skip-chars-backward "\n\r\t ")
	(setq target-lines (count-lines (point-min) (point)))
	;; Don't forget to count the last line.
	(if (not (bolp))
	    (setq target-lines (1+ target-lines))))
      (if (<= (window-height window) (* 2 window-min-height))
	  ;; At this point, every window on the frame is too small to split.
	  (setq w2 (display-buffer buf))
	(setq w2 (split-window
		  window
		  (max window-min-height
		       (- (window-height window)
			  (1+ (max window-min-height target-lines)))))))
      (set-window-buffer w2 buf)
      (if (< (1- (window-height w2)) target-lines)
	  (progn
	    (select-window w2)
	    (enlarge-window (- target-lines (1- (window-height w2))))))
      (set-window-start w2 1))))

(defun dired-mark-pop-up (bufname op-symbol files function &rest args)
  ;; Args BUFNAME OP-SYMBOL FILES FUNCTION &rest ARGS.
  ;; Return FUNCTION's result on ARGS after popping up a window (in a buffer
  ;; named BUFNAME, nil gives \" *Marked Files*\") showing the marked
  ;; files.  Uses function `dired-pop-to-buffer' to do that.
  ;; FUNCTION should not manipulate files.
  ;; It should only read input (an argument or confirmation).
  ;; The window is not shown if there is just one file or
  ;; OP-SYMBOL is a member of the list in `dired-no-confirm'.
  ;; FILES is the list of marked files.
  (if (memq op-symbol dired-no-confirm)
      (apply function args)
    (or bufname (setq bufname  " *Marked Files*"))
    (if (<= (length files) 1)
	(apply function args)
      (save-excursion
	(let ((standard-output (set-buffer (get-buffer-create bufname))))
	  (erase-buffer)
	  (dired-format-columns-of-files files)
	  (dired-remove-text-properties (point-min) (point-max))
	  (setq mode-line-format (format "       %s  [%d files]"
					 bufname (length files)))))
      (save-window-excursion
	(dired-pop-to-buffer bufname)
	(apply function args)))))

(defun dired-column-widths (columns list &optional across)
  ;; Returns the column widths for breaking LIST into
  ;; COLUMNS number of columns.
  (cond
   ((null list)
    nil)
   ((= columns 1)
    (list (apply 'max (mapcar 'length list))))
   ((let* ((len (length list))
	   (col-length (/ len columns))
	   (remainder (% len columns))
	   (i 0)
	   (j 0)
	   (max-width 0)
	   widths padding)
      (if (zerop remainder)
	  (setq padding 0)
	(setq col-length (1+ col-length)
	      padding (- columns remainder)))
      (setq list (nconc (copy-sequence list) (make-list padding nil)))
      (setcdr (nthcdr (1- (+ len padding)) list) list)
      (while (< i columns)
	(while (< j col-length)
	  (setq max-width (max max-width (length (car list)))
		list (if across (nthcdr columns list) (cdr list))
		j (1+ j)))
	(setq widths (cons (+ max-width 2) widths)
	      max-width 0
	      j 0
	      i (1+ i))
	(if across (setq list (cdr list))))
      (setcar widths (- (car widths) 2))
      (nreverse widths)))))
  
(defun dired-calculate-columns (list &optional across)
  ;; Returns a list of integers which are the column widths that best pack
  ;; LIST, a list of strings, onto the screen.
  (and list
       (let* ((width (1- (window-width)))
	      (columns (max 1 (/ width
				 (+ 2 (apply 'max (mapcar 'length list))))))
	      col-list last-col-list)
	 (while (<= (apply '+ (setq col-list
				    (dired-column-widths columns list across)))
		    width)
	   (setq columns (1+ columns)
		 last-col-list col-list))
	 (or last-col-list col-list))))

(defun dired-format-columns-of-files (files &optional across)
  ;; Returns the number of lines used.
  ;; If ACROSS is non-nil, sorts across rather than down the buffer, like
  ;; ls -x
  (and files
       (let* ((columns (dired-calculate-columns files across))
	      (ncols (length columns))
	      (ncols1 (1- ncols))
	      (nfiles (length files))
	      (nrows (+ (/ nfiles ncols)
			(if (zerop (% nfiles ncols)) 0 1)))
	      (space-left (- (window-width) (apply '+ columns) 1))
	      (i 0)
	      (j 0)
	      file padding stretch float-stretch)
	 (if (zerop ncols1)
	     (setq stretch 0
		   float-stretch 0)
	   (setq stretch (/ space-left ncols1)
		 float-stretch (% space-left ncols1)))
	 (setq files (nconc (copy-sequence files) ; fill up with empty fns
			    (make-list (- (* ncols nrows) nfiles) "")))
	 (setcdr (nthcdr (1- (length files)) files) files) ; make circular
	 (while (< j nrows)
	   (while (< i ncols)
	     (princ (setq file (car files)))
	     (setq padding (- (nth i columns) (length file)))
	     (or (= i ncols1)
		 (progn
		   (setq padding (+ padding stretch))
		   (if (< i float-stretch) (setq padding (1+ padding)))))
	     (princ (make-string padding ?\ ))
	     (setq files (if across (cdr files) (nthcdr nrows files))
		   i (1+ i)))
	   (princ "\n")
	   (setq i 0
		 j (1+ j))
	   (or across (setq files (cdr files))))
	 nrows)))

(defun dired-query (qs-var qs-prompt &rest qs-args)
  ;; Query user and return nil or t.
  ;; Store answer in symbol VAR (which must initially be bound to nil).
  ;; Format PROMPT with ARGS.
  ;; Binding variable help-form will help the user who types C-h.
  (let* ((char (symbol-value qs-var))
	 (action (cdr (assoc char dired-query-alist))))
    (cond ((eq 'yes action)
	   t)				; accept, and don't ask again
	  ((eq 'no action)
	   nil)				; skip, and don't ask again
	  (t;; no lasting effects from last time we asked - ask now
	   (let ((qprompt (concat qs-prompt
				  (if help-form
				      (format " [yn!q or %s] "
					      (key-description
					       (char-to-string help-char)))
				    " [ynq or !] ")))
		 (dired-in-query t)
		 elt)
	     ;; Actually it looks nicer without cursor-in-echo-area - you can
	     ;; look at the dired buffer instead of at the prompt to decide.
	     (apply 'message qprompt qs-args)
	     (setq char (set qs-var (read-char)))
	     (while (not (setq elt (assoc char dired-query-alist)))
	       (message "Invalid char - type %c for help." help-char)
	       (ding)
	       (sit-for 1)
	       (apply 'message qprompt qs-args)
	       (setq char (set qs-var (read-char))))
	     (memq (cdr elt) '(t y yes)))))))

(defun dired-mark-confirm (op-symbol operation arg)
  ;; Request confirmation from the user that the operation described
  ;; by OP-SYMBOL is to be performed on the marked files.
  ;; Confirmation consists in a y-or-n question with a file list
  ;; pop-up unless OP-SYMBOL is a member of `dired-no-confirm'.
  ;; OPERATION is a string describing the operation. Used for prompting
  ;; the user.
  ;; The files used are determined by ARG (like in dired-get-marked-files).
  (or (memq op-symbol dired-no-confirm)
      (let ((files (dired-get-marked-files t arg)))
	(dired-mark-pop-up nil op-symbol files (function y-or-n-p)
			   (concat  operation " "
				    (dired-mark-prompt arg files) "? ")))))

(defun dired-mark-read-file-name (prompt dir op-symbol arg files)
  (dired-mark-pop-up
   nil op-symbol files
   (function read-file-name)
   (format prompt (dired-mark-prompt arg files)) dir))

(defun dired-mark-read-string (prompt initial op-symbol arg files
				      &optional history-sym)
  ;; Reading arguments with history.
  ;; Read arguments for a mark command of type OP-SYMBOL,
  ;; perhaps popping up the list of marked files.
  ;; ARG is the prefix arg and indicates whether the files came from
  ;; marks (ARG=nil) or a repeat factor (integerp ARG).
  ;; If the current file was used, the list has but one element and ARG
  ;; does not matter. (It is non-nil, non-integer in that case, namely '(4)).
  ;; PROMPT for a string, with INITIAL input.
  (dired-mark-pop-up
   nil op-symbol files
   (function
    (lambda (prompt initial)
      (let ((hist (or history-sym
		      (cdr (assq op-symbol dired-op-history-alist))
		      'dired-history)))
	(dired-read-with-history prompt initial hist))))
   (format prompt (dired-mark-prompt arg files)) initial))


;;;; ----------------------------------------------------------
;;;; Marking files.
;;;; ----------------------------------------------------------

(defun dired-mark (arg &optional char)
  "Mark the current (or next ARG) files.
If on a subdir headerline, mark all its files except `.' and `..'.

Use \\[dired-unmark-all-files] to remove all marks,
and \\[dired-unmark] to remove the mark of the current file."
  (interactive "p")
  (if (dired-get-subdir)
      (dired-mark-subdir-files char)
    (dired-mark-file arg char)))

(defun dired-mark-file (arg &optional char)
  "Mark ARG files starting from the current file line.
Optional CHAR indicates a marker character to use."
  (let (buffer-read-only)
    (if (memq (or char dired-marker-char) '(?\  ?\n ?\r))
	(error "Invalid marker character %c" dired-marker-char))
    (or char (setq char dired-marker-char))
    (dired-repeat-over-lines
     arg
     (function
      (lambda ()
	(dired-update-marker-counters (char-after (point)) t)
	(dired-substitute-marker (point) (char-after (point)) char)
	(dired-update-marker-counters char))))
    (dired-update-mode-line-modified)))

(defun dired-mark-subdir-files (&optional char)
  "Mark all files except `.' and `..'."
  (interactive)
  (save-excursion
    (dired-mark-files-in-region (dired-subdir-min) (dired-subdir-max) char)))

(defun dired-unmark (arg)
  "Unmark the current (or next ARG) files.
If looking at a subdir, unmark all its files except `.' and `..'."
  (interactive "p")
  (let (buffer-read-only)
    (dired-repeat-over-lines
     arg
     (function
      (lambda ()
	(let ((char (char-after (point))))
	  (or (memq char '(?\  ?\n ?\r))
	      (progn
		(cond
		 ((char-equal char dired-marker-char)
		  (setq dired-marks-number (max (1- dired-marks-number) 0)))
		 ((char-equal char dired-del-marker)
		  (setq dired-del-flags-number
			(max (1- dired-del-flags-number) 0)))
		 ((setq dired-other-marks-number
			(max (1- dired-other-marks-number) 0))))
		(dired-substitute-marker (point) char ?\ )))))))
    (dired-update-mode-line-modified)))

(defun dired-mark-prefix (&optional arg)
  "Mark the next ARG files with the next character typed.
If ARG is negative, marks the previous files."
  (interactive "p")
  (if (sit-for echo-keystrokes)
      (cond
       ((or (= arg 1) (zerop arg))
	(message "Mark with character?"))
       ((< arg 0)
	(message "Mark %d file%s moving backwards?"
		 (- arg) (dired-plural-s (- arg))))
       ((> arg 1)
	(message "Mark %d following files with character?" arg))))
  (dired-mark arg (read-char)))

(defun dired-change-marks (old new)
  "Change all OLD marks to NEW marks.
OLD and NEW are both characters used to mark files.
With a prefix, prompts for a mark to toggle. In other words, all unmarked
files receive that mark, and all files currently marked with that mark become
unmarked."
  ;; When used in a lisp program, setting NEW to nil means toggle the mark OLD.
  (interactive
   (let* ((cursor-in-echo-area t)
	  (old nil)
	  (new nil)
	  (markers (dired-mark-list))
	  (default (cond ((null markers)
			  (error "No markers in buffer"))
			 ((= (length markers) 1)
			  (setq old (car markers)))
			 ((memq dired-marker-char markers)
			  dired-marker-char)
			 ;; picks the last one in the buffer. reasonable?
			 ((car markers)))))
     (or old (setq old
		   (progn
		     (if current-prefix-arg
			 (message "Toggle mark (default %c): " default)
		       (message "Change old mark (default %c): " default))
		     (read-char))))
     (if (memq old '(?\  ?\n ?\r)) (setq old default))
     (or current-prefix-arg
	 (setq new (progn
		     (message
		      "Change %c marks to new mark (RET means abort): " old)
		     (read-char))))
     (list old new)))
  (let ((old-count  (cond
		     ((char-equal old dired-marker-char)
		      'dired-marks-number)
		     ((char-equal old dired-del-marker)
		      'dired-del-flags-number)
		     ('dired-other-marks-number))))
    (if new
	(or (memq new '(?\  ?\n ?\r))
	    ;; \n and \r aren't valid marker chars. Assume that if the
	    ;; user hits return, he meant to abort the command.
	    (let ((string (format "\n%c" old))
		  (new-count  (cond
			       ((char-equal new dired-marker-char)
				'dired-marks-number)
			       ((char-equal new dired-del-marker)
				'dired-del-flags-number)
			       ('dired-other-marks-number)))
		  (buffer-read-only nil))
	      (save-excursion
		(goto-char (point-min))
		(while (search-forward string nil t)
		  (if (equal (char-before) old)
		      (progn
			(dired-substitute-marker (1- (point)) old new)
			(set new-count (1+ (symbol-value new-count)))
			(set old-count (max (1- (symbol-value old-count)) 0))))
		  ))))
      (save-excursion
	(let ((ucount 0)
	      (mcount 0)
	      (buffer-read-only nil))
	  (goto-char (point-min))
	  (while (not (eobp))
	    (or (dired-between-files)
		(looking-at dired-re-dot)
		(cond
		 ((equal (char-after (point)) ?\ )
		  (setq mcount (1+ mcount))
		  (set old-count (1+ (symbol-value old-count)))
		  (dired-substitute-marker (point) ?\  old))
		 ((equal (char-after (point)) old)
		  (setq ucount (1+ ucount))
		  (set old-count (max (1- (symbol-value old-count)) 0))
		  (dired-substitute-marker (point) old ?\ ))))
	    (forward-line 1))
	  (message "Unmarked %d file%s; marked %d file%s with %c."
		   ucount (dired-plural-s ucount) mcount
		   (dired-plural-s mcount) old)))))
  (dired-update-mode-line-modified))

(defun dired-unmark-all-files (flag &optional arg)
  "Remove a specific mark or any mark from every file.
With prefix arg, query for each marked file.
Type \\[help-command] at that time for help.
With a zero prefix, only counts the number of marks."
  (interactive
   (let* ((cursor-in-echo-area t)
	  executing-kbd-macro) ; for XEmacs
     (list (and (not (eq current-prefix-arg 0))
		(progn (message "Remove marks (RET means all): ") (read-char)))
	   current-prefix-arg)))
  (save-excursion
    (let* ((help-form "\
Type SPC or `y' to unflag one file, DEL or `n' to skip to next,
`!' to unflag all remaining files with no more questions.")
	   (allp (memq flag '(?\n ?\r)))
	   (count-p (eq arg 0))
	   (count (if (or allp count-p)
		      (mapcar
		       (function
			(lambda (elt)
			(cons elt 0)))
		       (nreverse (dired-mark-list)))
		    0))
	   (msg "")
	   (no-query (or (not arg) count-p))
	   buffer-read-only case-fold-search query)
      (goto-char (point-min))
      (if (or allp count-p)
	  (while (re-search-forward dired-re-mark nil t)
	    (if (or no-query
		    (dired-query 'query "Unmark file `%s'? "
				 (dired-get-filename t)))
		(let ((ent (assq (char-before) count)))
		  (if ent (setcdr ent (1+ (cdr ent))))
		  (or count-p (dired-substitute-marker
			       (- (point) 1) (char-before) ?\ ))))
	    (forward-line 1))
	(while (search-forward (format "\n%c" flag) nil t)
	  (if (or no-query
		  (dired-query 'query "Unmark file `%s'? "
			       (dired-get-filename t)))
	      (progn
		(dired-substitute-marker (1+ (match-beginning 0)) flag ?\ )
		(setq count (1+ count))))))
      (if (or allp count-p)
	  (mapcar
	   (function
	    (lambda (elt)
	      (or (zerop (cdr elt))
		  (setq msg (format "%s%s%d %c%s"
				    msg
				    (if (zerop (length msg))
					" "
				      ", ")
				    (cdr elt)
				    (car elt)
				    (if (= 1 (cdr elt)) "" "'s"))))))
	   count)
	(or (zerop count)
	    (setq msg (format " %d %c%s"
			      count flag (if (= 1 count) "" "'s")))))
      (if (zerop (length msg))
	  (setq msg " none")
	(or count-p (dired-update-mode-line-modified t)))
      (message "%s:%s" (if count-p "Number of marks" "Marks removed") msg))))

(defun dired-get-marked-files (&optional localp arg)
  "Return the marked files' names as list of strings.
The list is in the same order as the buffer, that is, the car is the
  first marked file.
Values returned are normally absolute pathnames.
Optional arg LOCALP as in `dired-get-filename'.
Optional second argument ARG forces to use other files.  If ARG is an
  integer, use the next ARG files.  If ARG is otherwise non-nil, use
  current file.  Usually ARG comes from the current prefix arg."
  (save-excursion
    (nreverse (dired-map-over-marks (dired-get-filename localp) arg))))

;;; Utility functions for marking files

(defun dired-mark-files-in-region (start end &optional char)
  (let (buffer-read-only)
    (if (> start end)
	(error "start > end"))
    (goto-char start)			; assumed at beginning of line
    (or char (setq char dired-marker-char))
    (while (< (point) end)
      ;; Skip subdir line and following garbage like the `total' line:
      (while (and (< (point) end) (dired-between-files))
	(forward-line 1))
      (if (and (not (equal (char-after (point)) char))
	       (not (looking-at dired-re-dot))
	       (save-excursion
		 (dired-move-to-filename nil (point))))
	  (progn
	    (dired-update-marker-counters (char-after (point)) t)
	    (dired-substitute-marker (point) (char-after (point)) char)
	    (dired-update-marker-counters char)))
      (forward-line 1)))
  (dired-update-mode-line-modified))

(defun dired-mark-list ()
  ;; Returns a list of all marks currently used in the buffer.
  (let ((result nil)
	char)
    (save-excursion
      (goto-char (point-min))
      (while (not (eobp))
	(and (not (memq (setq char (char-after (point))) '(?\  ?\n ?\r)))
	     (not (memq char result))
	     (setq result (cons char result)))
	(forward-line 1)))
    result))

;;; Dynamic markers

(defun dired-set-current-marker-string ()
  "Computes and returns `dired-marker-string'."
  (prog1
      (setq dired-marker-string
	    (if dired-marker-stack
		(let* ((n (+ (length dired-marker-stack) 5))
		       (str (make-string n ?\ ))
		       (list dired-marker-stack)
		       (pointer dired-marker-stack-pointer))
		  (setq n (1- n))
		  (aset str n ?\])
		  (setq n (1- n))
		  (while list
		    (aset str n (car list))
		    (if (zerop pointer)
			(progn
			  (setq n (1- n))
			  (aset str n dired-marker-stack-cursor)))
		    (setq n (1- n)
			  pointer (1- pointer)
			  list (cdr list)))
		  (aset str n dired-default-marker)
		  (if (zerop pointer)
		      (aset str 2 dired-marker-stack-cursor))
		  (aset str 1 ?\[)
		  str)
	      ""))
    (set-buffer-modified-p (buffer-modified-p))))

(defun dired-set-marker-char (c)
  "Set the marker character to something else.
Use \\[dired-restore-marker-char] to restore the previous value."
  (interactive "cNew marker character: ")
  (and (memq c '(?\  ?\n ?\r)) (error "invalid marker char %c" c))
  (setq dired-marker-stack (cons c dired-marker-stack)
	dired-marker-stack-pointer 0
	dired-marker-char c)
  (dired-update-mode-line-modified t)
  (dired-set-current-marker-string))

(defun dired-restore-marker-char ()
  "Restore the marker character to its previous value.
Uses `dired-default-marker' if the marker stack is empty."
  (interactive)
  (setq dired-marker-stack (cdr dired-marker-stack)
	dired-marker-char (car dired-marker-stack)
	dired-marker-stack-pointer (min dired-marker-stack-pointer
					(length dired-marker-stack)))
  (or dired-marker-char
      (setq dired-marker-char dired-default-marker))
  (dired-set-current-marker-string)
  (dired-update-mode-line-modified t)
  (or dired-marker-stack (message "Marker is %c" dired-marker-char)))

(defun dired-marker-stack-left (n)
  "Moves the marker stack cursor to the left."
  (interactive "p")
  (let ((len (1+ (length dired-marker-stack))))
    (or dired-marker-stack (error "Dired marker stack is empty."))
    (setq dired-marker-stack-pointer
	  (% (+ dired-marker-stack-pointer n) len))
    (if (< dired-marker-stack-pointer 0)
	(setq dired-marker-stack-pointer (+ dired-marker-stack-pointer
					    len)))
    (dired-set-current-marker-string)
    (setq dired-marker-char
	  (if (= dired-marker-stack-pointer (1- len))
	      dired-default-marker
	    (nth dired-marker-stack-pointer dired-marker-stack))))
  (dired-update-mode-line-modified t))

(defun dired-marker-stack-right (n)
  "Moves the marker stack cursor to the right."
  (interactive "p")
  (dired-marker-stack-left (- n)))

;;; Commands to mark or flag files based on their characteristics or names.

(defun dired-mark-symlinks (&optional unflag-p)
  "Mark all symbolic links.
With prefix argument, unflag all those files."
  (interactive "P")
  (dired-check-ls-l)
  (let ((dired-marker-char (if unflag-p ?\040 dired-marker-char)))
    (dired-mark-if (looking-at dired-re-sym) "symbolic link"))
  (dired-update-mode-line-modified t))

(defun dired-mark-directories (&optional unflag-p)
  "Mark all directory file lines except `.' and `..'.
With prefix argument, unflag all those files."
  (interactive "P")
  (if dired-re-dir
      (progn
	(dired-check-ls-l)
	(let ((dired-marker-char (if unflag-p ?\040 dired-marker-char)))
	  (dired-mark-if (and (looking-at dired-re-dir)
			      (not (looking-at dired-re-dot)))
			 "directory file"))))
  (dired-update-mode-line-modified t))

(defun dired-mark-executables (&optional unflag-p)
  "Mark all executable files.
With prefix argument, unflag all those files."
  (interactive "P")
  (if dired-re-exe
      (progn
	(dired-check-ls-l)
	(let ((dired-marker-char (if unflag-p ?\040 dired-marker-char)))
	  (dired-mark-if (looking-at dired-re-exe) "executable file"))))
  (dired-update-mode-line-modified t))
    
(defun dired-flag-backup-files (&optional unflag-p)
  "Flag all backup files (names ending with `~') for deletion.
With prefix argument, unflag these files."
  (interactive "P")
  (dired-check-ls-l)
  (let ((dired-marker-char (if unflag-p ?\040 dired-del-marker)))
    (dired-mark-if
     (and (not (and dired-re-dir (looking-at dired-re-dir)))
	  (let ((fn (dired-get-filename t t)))
	    (if fn (backup-file-name-p fn))))
     "backup file"))
  (dired-update-mode-line-modified t))

(defun dired-flag-auto-save-files (&optional unflag-p)
  "Flag for deletion files whose names suggest they are auto save files.
A prefix argument says to unflag those files instead."
  (interactive "P")
  (let ((dired-marker-char (if unflag-p ?\040 dired-del-marker)))
    (dired-mark-if
     ;; It is less than general to check for ~ here,
     ;; but it's the only way this runs fast enough.
     (and (save-excursion (end-of-line)
			  (equal (char-before) ?#))
	  (not (and dired-re-dir (looking-at dired-re-dir)))
	  (let ((fn (dired-get-filename t t)))
	    (if fn (auto-save-file-name-p
		    (file-name-nondirectory fn)))))
     "auto save file"))
  (dired-update-mode-line-modified t))

(defun dired-mark-rcs-files (&optional unflag-p)
  "Mark all files that are under RCS control.
With prefix argument, unflag all those files.
Mentions RCS files for which a working file was not found in this buffer.
Type \\[dired-why] to see them again."
  ;; Returns failures, or nil on success.
  ;; Finding those with locks would require to peek into the ,v file,
  ;; depends slightly on the RCS version used and should be done
  ;; together with the Emacs RCS interface.
  ;; Unfortunately, there is no definitive RCS interface yet.
  (interactive "P")
  (message "%sarking RCS controlled files..." (if unflag-p "Unm" "M"))
  (let ((dired-marker-char (if unflag-p ?\  dired-marker-char))
	rcs-files wf failures count total)
    (mapcar				; loop over subdirs
     (function
      (lambda (dir)
	(or (equal (file-name-nondirectory (directory-file-name dir))
		   "RCS")
	    ;; skip inserted RCS subdirs
	    (setq rcs-files
		  (append (directory-files dir t ",v\\'") ; *,v and RCS/*,v
			  (let ((rcs-dir (expand-file-name "RCS" dir)))
			    (if (file-directory-p rcs-dir)
				(mapcar	; working files from ./RCS are in ./
				 (function
				  (lambda (x)
				    (expand-file-name x dir)))
				 (directory-files
				  (file-name-as-directory rcs-dir)
				  nil ",v\\'"))))
			  rcs-files)))))
     (mapcar (function car) dired-subdir-alist))
    (setq total (length rcs-files))
    (while rcs-files
      (setq wf (substring (car rcs-files) 0 -2)
	    rcs-files (cdr rcs-files))
      (save-excursion (if (dired-goto-file wf)
			  (dired-mark 1) ; giving a prefix avoids checking
					 ; for subdir line.
			(setq failures (cons wf failures)))))
    (dired-update-mode-line-modified t)
    (if (null failures)
	(message "%d RCS file%s %smarked."
		 total (dired-plural-s total) (if unflag-p "un" ""))
      (setq count (length failures))
      (dired-log-summary (buffer-name (current-buffer))
			 "RCS working file not found %s" failures)
      (message "%d RCS file%s: %d %smarked - %d not found %s."
	       total (dired-plural-s total) (- total count)
	       (if unflag-p "un" "") count failures))
    failures))


;;;; ------------------------------------------------------------
;;;; Logging failures
;;;; ------------------------------------------------------------

(defun dired-why ()
  "Pop up a buffer with error log output from Dired.
A group of errors from a single command ends with a formfeed.
Thus, use \\[backward-page] to find the beginning of a group of errors."
  (interactive)
  (if (get-buffer dired-log-buffer)
      (let ((owindow (selected-window))
	    (window (display-buffer (get-buffer dired-log-buffer))))
	(unwind-protect
	    (progn
	      (select-window window)
	      (goto-char (point-max))
	      (recenter -1))
	  (select-window owindow)))))

(defun dired-log (buffer-name log &rest args)
  ;; Log a message or the contents of a buffer.
  ;; BUFFER-NAME is the name of the dired buffer to which the message applies.
  ;; If LOG is a string and there are more args, it is formatted with
  ;; those ARGS.  Usually the LOG string ends with a \n.
  ;; End each bunch of errors with (dired-log t): this inserts
  ;; current time and buffer, and a \f (formfeed).
  (or (stringp buffer-name) (setq buffer-name (buffer-name buffer-name)))
  (let ((obuf (current-buffer)))
    (unwind-protect			; want to move point
	(progn
	  (set-buffer (get-buffer-create dired-log-buffer))
	  (goto-char (point-max))
	  (let (buffer-read-only)
	    (cond ((stringp log)
		   (insert (if args
			       (apply (function format) log args)
			     log)))
		  ((bufferp log)
		   (insert-buffer log))
		  ((eq t log)
		   (insert "\n\t" (current-time-string)
			   "\tBuffer `" buffer-name "'\n\f\n")))))
      (set-buffer obuf))))

(defun dired-log-summary (buffer-name string failures)
  (message (if failures "%s--type y for details %s"
	     "%s--type y for details")
	   string failures)
  ;; Log a summary describing a bunch of errors.
  (dired-log buffer-name (concat "\n" string))
  (if failures (dired-log buffer-name "\n%s" failures))
  (dired-log buffer-name t))


;;;; -------------------------------------------------------
;;;; Sort mode of dired buffers.
;;;; -------------------------------------------------------

(defun dired-sort-type (list)
  ;; Returns the sort type of LIST, as a symbol.
  (let* ((list (reverse list))
	 (alist (sort
		 (mapcar (function
			  (lambda (x)
			    (cons (length (memq (car x) list)) (cdr x))))
			 dired-sort-type-alist)
		 (function
		  (lambda (x y)
		    (> (car x) (car y))))))
	 (winner (car alist)))
    (if (zerop (car winner))
	'name
      (cdr winner))))

(defun dired-sort-set-modeline (&optional switches)
  ;; Set modeline display according to dired-internal-switches.
  ;; Modeline display of "by name" or "by date" guarantees the user a
  ;; match with the corresponding regexps.  Non-matching switches are
  ;; shown literally.
  (or switches (setq switches dired-internal-switches))
  (setq dired-sort-mode
	(if dired-show-ls-switches
	    (concat " " (dired-make-switches-string
			 (or switches dired-internal-switches)))
	  (concat " by " (and (memq ?r switches) "rev-")
		  (symbol-name (dired-sort-type switches)))))
  ;; update mode line
  (set-buffer-modified-p (buffer-modified-p)))

(defun dired-sort-toggle-or-edit (&optional arg)
  "Toggle between sort by date/name for the current subdirectory.

With a 0 prefix argument, simply reports on the current switches.

With a prefix 1 allows the ls switches for the current subdirectory to be 
edited.

With a prefix 2 allows the default ls switches for newly inserted
subdirectories to be edited. 

With a prefix \\[universal-argument] allows you to sort the entire
buffer by either name or date.

With a prefix \\[universal-argument] \\[universal-argument] allows the default switches
for the entire buffer to be edited, and then reverts the buffer so that all
subdirectories are sorted according to these switches.

Note that although dired allows different ls switches to be used for 
different subdirectories, certain combinations of ls switches are incompatible.
If incompatible switches are detected, dired will offer to revert the buffer
to force the ls switches for all subdirectories to a single value.  If you
refuse to revert the buffer, any change of ls switches will be aborted."
  (interactive "P")
  (cond
   ((eq arg 0)
    ;; Report on switches
    (message "Switches for current subdir: %s.  Default for buffer: %s."
	     (dired-make-switches-string
	      (nth 3 (assoc (dired-current-directory) dired-subdir-alist)))
	      (dired-make-switches-string dired-internal-switches)))
   ((null arg)
    ;; Toggle between sort by date/name.
    (let* ((dir (dired-current-directory))
	   (curr (nth 3 (assoc dir dired-subdir-alist))))
      (dired-sort-other
       (if (eq (dired-sort-type curr) 'name)
	   (cons ?t curr)
	 (mapcar (function
		  (lambda (x)
		    (setq curr
			  (delq (car x) curr))))
		 dired-sort-type-alist)
	 curr)
       nil dir)))
   ((eq arg 1)
    ;; Edit switches for current subdir.
    (let* ((dir (dired-current-directory))
	   (switch-string
	    (read-string
	     "New ls switches for current subdir (must contain -l): "
	     (dired-make-switches-string
	      (nth 3 (assoc dir dired-subdir-alist)))))
	   (switches (dired-make-switches-list switch-string)))
      (if (dired-compatible-switches-p switches dired-internal-switches)
	  (dired-sort-other switches nil dir)
	(if (or
	     (memq 'sort-revert dired-no-confirm)
	     (y-or-n-p
	      (format
	       "Switches %s incompatible with default %s.  Revert buffer? "
	         switch-string
		 (dired-make-switches-string dired-internal-switches))))
	    (dired-sort-other switches nil nil)
	  (error "Switches unchanged.  Remain as %s." switch-string)))))
   ((eq arg 2)
    ;; Set new defaults for subdirs inserted in the future.
    (let* ((switch-string
	    (read-string
	     "Default ls switches for new subdirs (must contain -l): "
	     (dired-make-switches-string dired-internal-switches)))
	   (switches (dired-make-switches-list switch-string))
	   (alist dired-subdir-alist)
	   x bad-switches)
      (while alist
	(setq x (nth 3 (car alist))
	      alist (cdr alist))
	(or (dired-compatible-switches-p x switches)
	    (member x bad-switches)
	    (setq bad-switches (cons x bad-switches))))
      (if bad-switches
	  (if (or (memq 'sort-revert dired-no-confirm)
		  (y-or-n-p
		   (format
		    "Switches %s incompatible with %s.  Revert buffer? "
		    switch-string (mapconcat 'dired-make-switches-string
					      bad-switches ", "))))
	      (dired-sort-other switches nil nil)
	    (error "Default switches unchanged.  Remain as %s."
		   (dired-make-switches-string dired-internal-switches)))
	(dired-sort-other switches t nil))))
   ((or (equal arg '(4)) (eq arg 'date) (eq arg 'name))
    ;; Toggle the entire buffer name/data.
    (let ((cursor-in-echo-area t)
	  (switches (copy-sequence dired-internal-switches))
	  (type (and (symbolp arg) arg))
	  char)
      (while (null type)
	(message "Sort entire buffer according to (n)ame or (d)ate? ")
	(setq char (read-char)
	      type (cond
		    ((char-equal char ?d) 'date)
		    ((char-equal char ?n) 'name)
		    (t (message "Type one of n or d.") (sit-for 1) nil))))
      (mapcar (function
	       (lambda (x)
		 (setq switches
		       (delq (car x) switches))))
	      dired-sort-type-alist)
      (dired-sort-other
       (if (eq type 'date) (cons ?t switches) switches) nil nil)))
   ((equal arg '(16))
    ;; Edit the switches for the entire buffer.
    (dired-sort-other
     (dired-make-switches-list
      (read-string
       "Change ls switches for entire buffer to (must contain -l): "
       (dired-make-switches-string dired-internal-switches)))
     nil nil))
   (t
    ;; No idea what's going on.
    (error
     "Invalid prefix.  See %s dired-sort-toggle-or-edit."
     (substitute-command-keys
      (if (featurep 'ehelp)
	  "\\[electric-describe-function]"
	"\\[describe-function]"))))))

(defun dired-sort-other (switches &optional no-revert subdir)
  ;; Specify new ls SWITCHES for current dired buffer.
  ;; With optional second arg NO-REVERT, don't refresh the listing afterwards.
  ;; If subdir is non-nil, only changes the switches for the
  ;; sudirectory.
  (if subdir
      (let ((elt (assoc subdir dired-subdir-alist)))
	(if elt (setcar (nthcdr 3 elt) switches)))
    (setq dired-internal-switches switches))
  (or no-revert
      (cond
       
       (subdir
	(let ((ofile (dired-get-filename nil t))
	      (opoint (point)))
	  (message "Relisting %s..." subdir)
	  (dired-insert-subdir subdir switches)
	  (message "Relisting %s... done" subdir)
	  (or (and ofile (dired-goto-file ofile)) (goto-char opoint))))
       
       ((memq ?R switches)
	;; We are replacing a buffer with a giant recursive listing.
	(let ((opoint (point))
	      (ofile (dired-get-filename nil t))
	      (hidden-subdirs (dired-remember-hidden))
	      (mark-alist (dired-remember-marks (point-min) (point-max)))
	      (kill-files-p (save-excursion
			      (goto-char (point))
			      (search-forward
			       (concat (char-to-string ?\r)
				       (regexp-quote
					(char-to-string
					 dired-kill-marker-char)))
			       nil t)))
	      (omit-files (nth 2 (nth (1- (length dired-subdir-alist))
				      dired-subdir-alist)))
	      buffer-read-only)
	  (dired-readin dired-directory (current-buffer)
			(or (consp dired-directory)
			    (null (file-directory-p dired-directory))))
	  (dired-mark-remembered mark-alist)	; mark files that were marked
	  (if kill-files-p (dired-do-hide dired-kill-marker-char))
	  (if omit-files
	      (dired-omit-expunge nil t))
	  ;; hide subdirs that were hidden
	  (save-excursion
	    (mapcar (function (lambda (dir)
				(if (dired-goto-subdir dir)
				    (dired-hide-subdir 1))))
		    hidden-subdirs))
	  ;; Try to get back to where we were
	  (or (and ofile (dired-goto-file ofile))
	      (goto-char opoint))
	  (dired-move-to-filename)))
       
       (t
	;; Clear all switches in the subdir alist
	(setq dired-subdir-alist
	      (mapcar (function
		       (lambda (x)
			 (setcar (nthcdr 3 x) nil)
			 x))
		      dired-subdir-alist))
	(revert-buffer nil t))))
  (dired-update-mode-line t))

(defun dired-compatible-switches-p (list1 list2)
  ;; Returns t if list1 and list2 are allowed as switches in the same
  ;; dired buffer.
  (and (eq (null (or (memq ?l list1) (memq ?o list1) (memq ?g list1)))
	   (null (or (memq ?l list2) (memq ?o list2) (memq ?g list2))))
       (eq (null (memq ?F list1)) (null (memq ?F list2)))
       (eq (null (memq ?p list1)) (null (memq ?p list2)))
       (eq (null (memq ?b list1)) (null (memq ?b list2)))))

(defun dired-check-ls-l (&optional switches)
  ;; Check for long-style listings
  (let ((switches (or switches dired-internal-switches)))
    (or (memq ?l switches) (memq ?o switches) (memq ?g switches)
	(error "Dired needs -l, -o, or -g in ls switches"))))


;;;; --------------------------------------------------------------
;;;; Creating new files.
;;;; --------------------------------------------------------------
;;;
;;;  The dired-create-files paradigm is used for copying, renaming,
;;;  compressing, and making hard and soft links.

(defun dired-file-marker (file)
  ;; Return FILE's marker, or nil if unmarked.
  (save-excursion
    (and (dired-goto-file file)
	 (progn
	   (skip-chars-backward "^\n\r")
	   (and (not (= ?\040 (char-after (point))))
		(char-after (point)))))))

;; The basic function for half a dozen variations on cp/mv/ln/ln -s.
(defun dired-create-files (file-creator operation fn-list name-constructor
					&optional marker-char query
					implicit-to)
  ;; Create a new file for each from a list of existing files.  The user
  ;; is queried, dired buffers are updated, and at the end a success or
  ;; failure message is displayed
  
  ;; FILE-CREATOR must accept three args: oldfile newfile ok-if-already-exists
  ;; It is called for each file and must create newfile, the entry of
  ;; which will be added.  The user will be queried if the file already
  ;; exists.  If oldfile is removed by FILE-CREATOR (i.e, it is a
  ;; rename), it is FILE-CREATOR's responsibility to update dired
  ;; buffers.  FILE-CREATOR must abort by signalling a file-error if it
  ;; could not create newfile.  The error is caught and logged.
  
  ;; OPERATION (a capitalized string, e.g. `Copy') describes the
  ;; operation performed.  It is used for error logging.
  
  ;; FN-LIST is the list of files to copy (full absolute pathnames).
  
  ;; NAME-CONSTRUCTOR returns a newfile for every oldfile, or nil to
  ;; skip.  If it skips files, it is supposed to tell why (using dired-log).

  ;; Optional MARKER-CHAR is a character with which to mark every
  ;; newfile's entry, or t to use the current marker character if the
  ;; oldfile was marked.

  ;; QUERY is a function to use to prompt the user about creating a file.
  ;; It accepts two  args, the from and to files,
  ;; and must return nil or t. If QUERY is nil, then no user
  ;; confirmation will be requested.

  ;; If IMPLICIT-TO is non-nil, then the file constructor does not take
  ;; a to-file arg. e.g. compress.

  (let ((success-count 0)
	(total (length fn-list))
	failures skipped overwrite-query)
    ;; Fluid vars used for storing responses of previous queries must be
    ;; initialized.
    (dired-save-excursion
      (setq dired-overwrite-backup-query nil
	    dired-file-creator-query nil)
      (mapcar
       (function
	(lambda (from)
	  (let ((to (funcall name-constructor from)))
	    (if to
		(if (equal to from)
		    (progn
		      (dired-log (buffer-name (current-buffer))
				 "Cannot %s to same file: %s\n"
				 (downcase operation) from)
		      (setq skipped (cons (dired-make-relative from) skipped)))
		  (if (or (null query)
			  (funcall query from to))
		      (let* ((overwrite (let (jka-compr-enabled)
					  ;; Don't let jka-compr fool us.
					  (file-exists-p to)))
			     ;; for dired-handle-overwrite
			     (dired-overwrite-confirmed 
			      (and overwrite
				   (let ((help-form '(format "\
Type SPC or `y' to overwrite file `%s',
DEL or `n' to skip to next,
ESC or `q' to not overwrite any of the remaining files,
`!' to overwrite all remaining files with no more questions." to)))
				     (dired-query 'overwrite-query
						  "Overwrite %s?" to))))
			     ;; must determine if FROM is marked before
			     ;; file-creator gets a chance to delete it
			     ;; (in case of a move).
			     (actual-marker-char
			      (cond  ((characterp marker-char) marker-char)
				     (marker-char (dired-file-marker from))
				     (t nil))))
			(if (and overwrite (null dired-overwrite-confirmed))
			    (setq skipped (cons (dired-make-relative from)
						skipped))
			  (condition-case err
			      (let ((dired-unhandle-add-files
				     (cons to dired-unhandle-add-files)))
				(if implicit-to
				    (funcall file-creator from
					     dired-overwrite-confirmed)
				  (funcall file-creator from to
					   dired-overwrite-confirmed))
				(setq success-count (1+ success-count))
				(message "%s: %d of %d"
					 operation success-count total)
				(dired-add-file to actual-marker-char))
			    (file-error		; FILE-CREATOR aborted
			     (progn
			       (setq failures (cons (dired-make-relative from)
						    failures))
			       (dired-log (buffer-name (current-buffer))
					  "%s `%s' to `%s' failed:\n%s\n"
					  operation from to err))))))
		    (setq skipped (cons (dired-make-relative from) skipped))))
	      (setq skipped (cons (dired-make-relative from) skipped))))))
       fn-list)
      (cond
       (failures
	(dired-log-summary
	 (buffer-name (current-buffer))
	 (format "%s failed for %d of %d file%s"
		 operation (length failures) total
		 (dired-plural-s total)) failures))
       (skipped
	(dired-log-summary
	 (buffer-name (current-buffer))
	 (format "%s: %d of %d file%s skipped"
		 operation (length skipped) total
		 (dired-plural-s total)) skipped))
       (t
	(message "%s: %s file%s."
		 operation success-count (dired-plural-s success-count)))))))
  
(defun dired-do-create-files (op-symbol file-creator operation arg
					     &optional marker-char
					     prompter how-to)
  ;; Create a new file for each marked file.
  ;; Prompts user for target, which is a directory in which to create
  ;;   the new files.  Target may be a plain file if only one marked
  ;;   file exists.
  ;; OP-SYMBOL is the symbol for the operation.  Function `dired-mark-pop-up'
  ;;   will determine wether pop-ups are appropriate for this OP-SYMBOL.
  ;; FILE-CREATOR and OPERATION as in dired-create-files.
  ;; ARG as in dired-get-marked-files.
  ;; PROMPTER is a function of one-arg, the list of files, to return a prompt
  ;;     to use for dired-read-file-name. If it is nil, then a default prompt
  ;;     will be used.
  ;; Optional arg MARKER-CHAR as in dired-create-files.
  ;; Optional arg HOW-TO determines how to treat target:
  ;;   If HOW-TO is not given (or nil), and target is a directory, the
  ;;     file(s) are created inside the target directory.  If target
  ;;     is not a directory, there must be exactly one marked file,
  ;;     else error.
  ;;   If HOW-TO is t, then target is not modified.  There must be
  ;;     exactly one marked file, else error.
  ;; Else HOW-TO is assumed to be a function of one argument, target,
  ;;     that looks at target and returns a value for the into-dir
  ;;     variable.  The function dired-into-dir-with-symlinks is provided
  ;;     for the case (common when creating symlinks) that symbolic
  ;;     links to directories are not to be considered as directories
  ;;     (as file-directory-p would if HOW-TO had been nil).

  (let* ((fn-list (dired-get-marked-files nil arg))
	 (fn-count (length fn-list))
	 (cdir (dired-current-directory))
	 (target (expand-file-name
		   (dired-mark-read-file-name
		    (if prompter
			(funcall prompter fn-list)
		      (concat operation " %s to: "))
		    (dired-dwim-target-directory)
		    op-symbol arg (mapcar (function
					   (lambda (fn)
					     (dired-make-relative fn cdir t)))
					  fn-list))))
	 (into-dir (cond ((null how-to) (file-directory-p target))
			 ((eq how-to t) nil)
			 (t (funcall how-to target)))))
    (if (and (> fn-count 1)
	     (not into-dir))
	(error "Marked %s: target must be a directory: %s" operation target))
    ;; rename-file bombs when moving directories unless we do this:
    (or into-dir (setq target (directory-file-name target)))
    (dired-create-files
     file-creator operation fn-list
     (if into-dir			; target is a directory
	 (list 'lambda '(from)
	       (list 'expand-file-name '(file-name-nondirectory from) target))
       (list 'lambda '(from) target))
     marker-char)))

(defun dired-into-dir-with-symlinks (target)
  (and (file-directory-p target)
       (not (file-symlink-p target))))
;; This may not always be what you want, especially if target is your
;; home directory and it happens to be a symbolic link, as is often the
;; case with NFS and automounters.  Or if you want to make symlinks
;; into directories that themselves are only symlinks, also quite
;; common.
;; So we don't use this function as value for HOW-TO in
;; dired-do-symlink, which has the minor disadvantage of
;; making links *into* a symlinked-dir, when you really wanted to
;; *overwrite* that symlink.  In that (rare, I guess) case, you'll
;; just have to remove that symlink by hand before making your marked
;; symlinks.

(defun dired-handle-overwrite (to)
  ;; Save old version of a to be overwritten file TO.
  ;; `dired-overwrite-confirmed' and `dired-overwrite-backup-query'
  ;; are fluid vars from dired-create-files.
  (if (and dired-backup-if-overwrite
	   dired-overwrite-confirmed
	   (or (eq 'always dired-backup-if-overwrite)
	       (dired-query 'dired-overwrite-backup-query
			(format "Make backup for existing file `%s'? " to))))
      (let ((backup (car (find-backup-file-name to))))
	(rename-file to backup 0))))	; confirm overwrite of old backup

(defun dired-dwim-target-directory ()
  ;; Try to guess which target directory the user may want.
  ;; If there is a dired buffer displayed in the next window, use
  ;; its current subdir, else use current subdir of this dired buffer.
  ;; non-dired buffer may want to profit from this function, e.g. vm-uudecode
  (let* ((this-dir (and (eq major-mode 'dired-mode)
			(dired-current-directory)))
	 (dwimmed
	  (if dired-dwim-target
	      (let* ((other-buf (window-buffer (next-window)))
		     (other-dir (save-excursion
				  (set-buffer other-buf)
				  (and (eq major-mode 'dired-mode)
				       (dired-current-directory)))))
		(or other-dir this-dir))
	    this-dir)))
    (and dwimmed (dired-abbreviate-file-name dwimmed))))

(defun dired-get-target-directory ()
  "Writes a copy of the current subdirectory into an active minibuffer."
  (interactive)
  (let ((mb (dired-get-active-minibuffer-window)))
    (if mb
	(let ((dir (dired-current-directory)))
	  (select-window mb)
	  (set-buffer (window-buffer mb))
	  (erase-buffer)
	  (insert dir))
      (error "No active minibuffer"))))

;;; Copying files

(defun dired-do-copy (&optional arg)
  "Copy all marked (or next ARG) files, or copy the current file.
When operating on just the current file, you specify the new name.
When operating on multiple or marked files, you specify a directory
and the files are copied into that directory, retaining the same file names.

A zero prefix argument copies nothing.  But it toggles the
variable `dired-copy-preserve-time' (which see)."
  (interactive "P")
  (if (not (zerop (prefix-numeric-value arg)))
      (dired-do-create-files 'copy (function dired-copy-file)
			       (if dired-copy-preserve-time "Copy [-p]" "Copy")
			       arg dired-keep-marker-copy)
    (setq dired-copy-preserve-time (not dired-copy-preserve-time))
    (if dired-copy-preserve-time
	(message "Copy will preserve time.")
      (message "Copied files will get current date."))))

(defun dired-copy-file (from to ok-flag)
  (dired-handle-overwrite to)
  (copy-file from to ok-flag dired-copy-preserve-time))

;;; Renaming/moving files

(defun dired-do-rename (&optional arg)
  "Rename current file or all marked (or next ARG) files.
When renaming just the current file, you specify the new name.
When renaming multiple or marked files, you specify a directory.

A zero ARG moves no files but toggles `dired-dwim-target' (which see)."
  (interactive "P")
  (if (not (zerop (prefix-numeric-value arg)))
      (dired-do-create-files 'move (function dired-rename-file)
			       "Move" arg dired-keep-marker-rename
			       (function
				(lambda (list)
				  (if (= (length list) 1)
				      "Rename %s to: "
				    "Move %s to: "))))
    (setq dired-dwim-target (not dired-dwim-target))
    (message "dired-dwim-target is %s." (if dired-dwim-target "ON" "OFF"))))

(defun dired-rename-file (from to ok-flag)
  (dired-handle-overwrite to)
  (let ((insert (assoc (file-name-as-directory from) dired-subdir-alist)))
    (rename-file from to ok-flag) ; error is caught in -create-files
    ;; Silently rename the visited file of any buffer visiting this file.
    (dired-rename-update-buffers from to insert)))

(defun dired-rename-update-buffers (from to &optional insert)
  (if (get-file-buffer from)
      (save-excursion
	(set-buffer (get-file-buffer from))
	(let ((modflag (buffer-modified-p)))
	  (set-visited-file-name to)	; kills write-file-hooks
	  (set-buffer-modified-p modflag)))
    ;; It's a directory.  More work to do.
    (let ((blist (buffer-list))
	  (from-dir (file-name-as-directory from))
	  (to-dir (file-name-as-directory to)))
      (save-excursion
	(while blist
	  (set-buffer (car blist))
	  (setq blist (cdr blist))
	  (cond
	   (buffer-file-name
	    (if (dired-in-this-tree buffer-file-name from-dir)
		(let ((modflag (buffer-modified-p)))
		  (unwind-protect
		      (set-visited-file-name
		       (concat to-dir (substring buffer-file-name
						 (length from-dir))))
		    (set-buffer-modified-p modflag)))))
	   (dired-directory
	    (if (string-equal from-dir (expand-file-name default-directory))
		;; If top level directory was renamed, lots of things
		;; have to be updated.
		(progn
		  (dired-unadvertise from-dir)
		  (setq default-directory to-dir
			dired-directory
			;; Need to beware of wildcards.
			(expand-file-name 
			 (file-name-nondirectory dired-directory)
			 to-dir))
		  (let ((new-name (file-name-nondirectory
				   (directory-file-name dired-directory))))
		    ;; Try to rename buffer, but just leave old name if new
		    ;; name would already exist (don't try appending "<%d>")
		    ;; Why?  --sandy 19-8-94
		    (or (get-buffer new-name)
			(rename-buffer new-name)))
		  (dired-advertise))
	      (and insert
		   (assoc (file-name-directory (directory-file-name to))
			  dired-subdir-alist)
		   (dired-insert-subdir to))))))))))

;;; Making symbolic links

(defun dired-do-symlink (&optional arg)
  "Make symbolic links to current file or all marked (or next ARG) files.
When operating on just the current file, you specify the new name.
When operating on multiple or marked files, you specify a directory
and new symbolic links are made in that directory
with the same names that the files currently have."
  (interactive "P")
  (dired-do-create-files 'symlink (function make-symbolic-link)
			   "SymLink" arg dired-keep-marker-symlink))

;; Relative symlinks:
;; make-symbolic no longer expands targets (as of at least 18.57),
;; so the code to call ln has been removed.

(defun dired-do-relsymlink (&optional arg)
  "Symlink all marked (or next ARG) files into a directory,
or make a symbolic link to the current file.
This creates relative symbolic links like

    foo -> ../bar/foo

not absolute ones like

    foo -> /ugly/path/that/may/change/any/day/bar/foo"
  (interactive "P")
  (dired-do-create-files 'relsymlink (function dired-make-relative-symlink)
			 "RelSymLink" arg dired-keep-marker-symlink))

(defun dired-make-relative-symlink (target linkname
					   &optional ok-if-already-exists)
  "Make a relative symbolic link pointing to TARGET with name LINKNAME.
Three arguments: FILE1 FILE2 &optional OK-IF-ALREADY-EXISTS
The link is relative (if possible), for example

    \"/vol/tex/bin/foo\" \"/vol/local/bin/foo\"

results in

    \"../../tex/bin/foo\" \"/vol/local/bin/foo\""
  (interactive
   (let ((target (read-string "Make relative symbolic link to file: ")))
     (list
      target
      (read-file-name (format "Make relsymlink to file %s: " target))
      0)))
  (let* ((target (expand-file-name target))
	 (linkname (expand-file-name linkname))
	 (handler (or (find-file-name-handler
		       linkname 'dired-make-relative-symlink)
		      (find-file-name-handler
		       target 'dired-make-relative-symlink))))
    (if handler
	(funcall handler 'dired-make-relative-symlink target linkname
		 ok-if-already-exists)
      (setq target (directory-file-name target)
	    linkname (directory-file-name linkname))
      (make-symbolic-link
       (dired-make-relative target (file-name-directory linkname) t)
       linkname ok-if-already-exists))))

;;; Hard links -- adding names to files

(defun dired-do-hardlink (&optional arg)
  "Add names (hard links) current file or all marked (or next ARG) files.
When operating on just the current file, you specify the new name.
When operating on multiple or marked files, you specify a directory
and new hard links are made in that directory
with the same names that the files currently have."
  (interactive "P")
  (dired-do-create-files 'hardlink (function add-name-to-file)
			   "HardLink" arg dired-keep-marker-hardlink))


;;;; ---------------------------------------------------------------
;;;; Running process on marked files
;;;; ---------------------------------------------------------------
;;;
;;;  Commands for shell processes are in dired-shell.el.

;;; Internal functions for running subprocesses,
;;; checking and logging of their errors.

(defun dired-call-process (program discard &rest arguments)
  ;; Run PROGRAM with output to current buffer unless DISCARD is t.
  ;; Remaining arguments are strings passed as command arguments to PROGRAM.
  ;; Returns program's exit status, as an integer.
  ;; This is a separate function so that efs can redefine it.
  (let ((return
	 (apply 'call-process program nil (not discard) nil arguments)))
    (if (and (not (equal shell-file-name program))
	     (integerp return))
	return
      ;; Fudge return code by looking for errors in current buffer.
      (if (zerop (buffer-size)) 0 1))))
	  
(defun dired-check-process (msg program &rest arguments)
  ;; Display MSG while running PROGRAM, and check for output.
  ;; Remaining arguments are strings passed as command arguments to PROGRAM.
  ;; On error, insert output in a log buffer and return the
  ;; offending ARGUMENTS or PROGRAM.
  ;; Caller can cons up a list of failed args.
  ;; Else returns nil for success.
  (let ((err-buffer (get-buffer-create " *dired-check-process output*"))
	(dir default-directory))
    (message "%s..." msg)
    (save-excursion
      ;; Get a clean buffer for error output:
      (set-buffer err-buffer)
      (erase-buffer)
      (setq default-directory dir)	; caller's default-directory
      (if (not
	   (eq 0 (apply (function dired-call-process) program nil arguments)))
	  (progn
	    (dired-log (buffer-name (current-buffer))
		       (concat program " " (prin1-to-string arguments) "\n"))
	    (dired-log (buffer-name (current-buffer)) err-buffer)
	    (or arguments program t))
	(kill-buffer err-buffer)
	(message "%s...done" msg)
	nil))))

;;; Changing file attributes

(defun dired-do-chxxx (attribute-name program op-symbol arg)
  ;; Change file attributes (mode, group, owner) of marked files and
  ;; refresh their file lines.
  ;; ATTRIBUTE-NAME is a string describing the attribute to the user.
  ;; PROGRAM is the program used to change the attribute.
  ;; OP-SYMBOL is the type of operation (for use in dired-mark-pop-up).
  ;; ARG describes which files to use, like in dired-get-marked-files.
  (let* ((files (dired-get-marked-files t arg))
	 (new-attribute
	  (dired-mark-read-string
	   (concat "Change " attribute-name " of %s to: ")
	   nil op-symbol arg files))
	 (operation (concat program " " new-attribute))
	 (failures
	  (dired-bunch-files 10000 (function dired-check-process)
			     (list operation program new-attribute)
			     files)))
    (dired-do-redisplay arg);; moves point if ARG is an integer
    (if failures
	(dired-log-summary (buffer-name (current-buffer))
			   (format "%s: error" operation) nil))))

(defun dired-do-chmod (&optional arg)
  "Change the mode of the marked (or next ARG) files.
This calls chmod, thus symbolic modes like `g+w' are allowed."
  (interactive "P")
  (dired-do-chxxx "Mode" "chmod" 'chmod arg))

(defun dired-do-chgrp (&optional arg)
  "Change the group of the marked (or next ARG) files."
  (interactive "P")
  (dired-do-chxxx "Group" "chgrp" 'chgrp arg))

(defun dired-do-chown (&optional arg)
  "Change the owner of the marked (or next ARG) files."
  (interactive "P")
  (dired-do-chxxx "Owner" dired-chown-program 'chown arg))

;;; Utilities for running processes on marked files.

;; Process all the files in FILES in batches of a convenient size,
;; by means of (FUNCALL FUNCTION ARGS... SOME-FILES...).
;; Batches are chosen to need less than MAX chars for the file names,
;; allowing 3 extra characters of separator per file name.
(defun dired-bunch-files (max function args files)
  (let (pending
	(pending-length 0)
	failures)
    ;; Accumulate files as long as they fit in MAX chars,
    ;; then process the ones accumulated so far.
    (while files
      (let* ((thisfile (car files))
	     (thislength (+ (length thisfile) 3))
	     (rest (cdr files)))
	;; If we have at least 1 pending file
	;; and this file won't fit in the length limit, process now.
	(if (and pending (> (+ thislength pending-length) max))
	    (setq failures
		  (nconc (apply function (append args pending))
			 failures)
		  pending nil
		  pending-length 0))
	;; Do (setq pending (cons thisfile pending))
	;; but reuse the cons that was in `files'.
	(setcdr files pending)
	(setq pending files)
	(setq pending-length (+ thislength pending-length))
	(setq files rest)))
    (nconc (apply function (append args pending))
	   failures)))


;;;; ---------------------------------------------------------------
;;;; Calculating data or properties for marked files.
;;;; ---------------------------------------------------------------

(defun dired-do-total-size (&optional arg)
  "Show total size of all marked (or next ARG) files."
  (interactive "P")
  (let* ((result (dired-map-over-marks (dired-get-file-size) arg))
	 (total (apply (function +) result))
	 (num (length result)))
    (message "%d bytes (%d kB) in %s file%s"
	     total (/ total 1024) num (dired-plural-s num))
    total))

(defun dired-get-file-size ()
  ;; Returns the file size in bytes of the current file, as an integer.
  ;; Assumes that it is on a valid file line. It's the caller's responsibility
  ;; to ensure this. Assumes that match 0 for dired-re-month-and-time is
  ;; at the end of the file size.
  (end-of-line)
  (search-backward-regexp dired-re-month-and-time)
  (skip-chars-backward " ")
  (string-to-int (buffer-substring (point)
				   (progn (skip-chars-backward "0-9")
					  (point)))))

(defun dired-copy-filenames-as-kill (&optional arg)
  "Copy names of marked (or next ARG) files into the kill ring.
The names are separated by a space, and may be copied into other buffers
with \\[yank].  The list of names is also stored in the variable 
`dired-marked-files' for possible manipulation in the *scratch* buffer.

With a 0 prefix argument, use the pathname relative to the top-level dired
directory for each marked file.

With a prefix \\[universal-argument], use the complete pathname of each 
marked file.

With a prefix \\[universal-argument] \\[universal-argument], copy the complete
file line.  In this case, the lines are separated by newlines.

If on a subdirectory headerline and no prefix argument given, use the
subdirectory name instead."
  (interactive "P")
  (let (res)
    (cond
     ((and (null arg) (setq res (dired-get-subdir)))
      (kill-new res)
      (message "Copied %s into kill ring." res))
     ((equal arg '(16))
      (setq dired-marked-files
	    (dired-map-over-marks
	     (concat " " ; Don't copy the mark.
		     (buffer-substring
		      (progn (beginning-of-line) (1+ (point)))
		      (progn (skip-chars-forward "^\n\r") (point))))
		     nil))
      (let ((len (length dired-marked-files)))
	(kill-new (concat
		   (mapconcat 'identity dired-marked-files "\n")
		   "\n"))
	(message "Copied %d file line%s into kill ring."
		 len (dired-plural-s len))))
     (t
      (setq dired-marked-files
	    (cond
	     ((null arg)
	      (dired-get-marked-files 'no-dir))
	     ((eq arg 0)
	      (dired-get-marked-files t))
	     ((integerp arg)
	      (dired-get-marked-files 'no-dir arg))
	     ((equal arg '(4))
	      (dired-get-marked-files))
	     (t (error "Invalid prefix %s" arg))))
      (let ((len (length dired-marked-files)))
	(kill-new (mapconcat 'identity dired-marked-files " "))
	(message "Copied %d file name%s into kill ring."
		 len (dired-plural-s len)))))))


;;;; -----------------------------------------------------------
;;;; Killing subdirectories
;;;; -----------------------------------------------------------
;;;
;;;  These commands actually remove text from the dired buffer.

(defun dired-kill-subdir (&optional remember-marks tree)
  "Remove all lines of current subdirectory.
Lower levels are unaffected.  If given a prefix when called interactively,
kills the entire directory tree below the current subdirectory."
  ;; With optional REMEMBER-MARKS, return a mark-alist.
  (interactive (list nil current-prefix-arg))
  (let ((cur-dir (dired-current-directory)))
    (if (string-equal cur-dir (expand-file-name default-directory))
	(error "Attempt to kill top level directory"))
    (if tree
	(dired-kill-tree cur-dir remember-marks)
      (let ((beg (dired-subdir-min))
	    (end (dired-subdir-max))
	    buffer-read-only)
	(prog1
	    (if remember-marks (dired-remember-marks beg end))
	  (goto-char beg)
	  (or (bobp) (forward-char -1)) ; gobble separator
	  (delete-region (point) end)
	  (dired-unsubdir cur-dir)
	  (dired-update-mode-line)
	  (dired-update-mode-line-modified t))))))

(defun dired-kill-tree (dirname &optional remember-marks)
  "Kill all proper subdirs of DIRNAME, excluding DIRNAME itself.
With optional arg REMEMBER-MARKS, return an alist of marked files."
  (interactive "DKill tree below directory: ")
  (let ((s-alist dired-subdir-alist) dir m-alist)
    (while s-alist
      (setq dir (car (car s-alist))
	    s-alist (cdr s-alist))
      (if (and (not (string-equal dir dirname))
	       (dired-in-this-tree dir dirname)
	       (dired-goto-subdir dir))
	  (setq m-alist (nconc (dired-kill-subdir remember-marks) m-alist))))
    (dired-update-mode-line)
    (dired-update-mode-line-modified t)
    m-alist))


;;;; ------------------------------------------------------------
;;;; Killing file lines
;;;; ------------------------------------------------------------
;;;
;;;  Uses selective diplay, rather than removing lines from the buffer.

(defun dired-do-kill-file-lines (&optional arg)
  "Kill all marked file lines, or those indicated by the prefix argument.
Killing file lines means hiding them with selective display.  Giving
a zero prefix redisplays all killed file lines."
  (interactive "P")
  (or selective-display
      (error "selective-display must be t for file line killing to work!"))
  (if (eq arg 0)
      (dired-do-unhide dired-kill-marker-char
		       "Successfully resuscitated %d file line%s."
		       dired-keep-marker-kill)
    (let ((files
	   (length
	    (dired-map-over-marks
	     (progn
	       (beginning-of-line)
	       (subst-char-in-region (1- (point)) (point) ?\n ?\r)
	       (dired-substitute-marker (point) (char-after (point))
					dired-kill-marker-char)
	       (dired-update-marker-counters dired-marker-char t)
	       t)
	     arg))))
      ;; Beware of extreme apparent save-excursion lossage here.
      (let ((opoint (point)))
	(skip-chars-backward "^\n\r")
	(if (equal (char-before) ?\n)
	    (goto-char opoint)
	  (setq opoint (- opoint (point)))
	  (beginning-of-line)
	  (skip-chars-forward "^\n\r" (+ (point) opoint))))
      (dired-update-mode-line-modified)
      (message "Killed %d file line%s." files (dired-plural-s files)))))


;;;; ----------------------------------------------------------------
;;;; Omitting files.
;;;; ----------------------------------------------------------------

;; Marked files are never omitted.
;; Adapted from code submitted by:
;; Michael D. Ernst, mernst@theory.lcs.mit.edu, 1/11/91
;; Changed to work with selective display by Sandy Rutherford, 13/12/92.
;; For historical reasons, we still use the term expunge, although nothing
;; is expunged from the buffer.

(defun dired-omit-toggle (&optional arg)
  "Toggle between displaying and omitting files matching
`dired-omit-regexps' in the current subdirectory.
With a positive prefix, omits files in the entire tree dired buffer.
With a negative prefix, forces all files in the tree dired buffer to be
displayed."
  (interactive "P")
  (if arg
      (let ((arg (prefix-numeric-value arg)))
	(if (>= arg 0)
	    (dired-omit-expunge nil t)
	  (dired-do-unhide dired-omit-marker-char "")
	  (mapcar
	   (function
	    (lambda (elt)
	      (setcar (nthcdr 2 elt) nil)))
	   dired-subdir-alist)))
    (if (dired-current-subdir-omitted-p)
	(save-restriction
	  (narrow-to-region (dired-subdir-min) (dired-subdir-max))
	  (dired-do-unhide dired-omit-marker-char "")
	  (setcar (nthcdr 2 (assoc
			     (dired-current-directory) dired-subdir-alist))
		  nil)
	  (setq dired-subdir-omit nil))
      (dired-omit-expunge)
      (setq dired-subdir-omit t)))
  (dired-update-mode-line t))

(defun dired-current-subdir-omitted-p ()
  ;; Returns t if the current subdirectory is omited.
  (nth 2 (assoc (dired-current-directory) dired-subdir-alist)))

(defun dired-remember-omitted ()
  ;; Returns a list of omitted subdirs.
  (let ((alist dired-subdir-alist)
	result elt)
    (while alist
      (setq elt (car alist)
	    alist (cdr alist))
      (if (nth 2 elt)
	  (setq result (cons (car elt) result))))
    result))

(defun dired-omit-expunge (&optional regexp full-buffer)
  ;; Hides all unmarked files matching REGEXP.
  ;; If REGEXP is nil or not specified, uses `dired-omit-regexps',
  ;; and also omits filenames ending in `dired-omit-extensions'.
  ;; If REGEXP is the empty string, this function is a no-op.
  (let ((omit-re (or regexp (dired-omit-regexp)))
	(alist dired-subdir-alist)
	elt min)
    (if (null omit-re)
	0
      (if full-buffer
	  (prog1
	      (dired-omit-region (point-min) (point-max) omit-re)
	    ;; Set omit property in dired-subdir-alist
	    (while alist
	      (setq elt (car alist)
		    min (dired-get-subdir-min elt)
		    alist (cdr alist))
	      (if (and (<= (point-min) min) (>= (point-max) min))
		  (setcar (nthcdr 2 elt) t))))
	(prog1
	    (dired-omit-region (dired-subdir-min) (dired-subdir-max) omit-re)
	  (setcar
	   (nthcdr 2 (assoc (dired-current-directory)
			    dired-subdir-alist))
	   t))))))

(defun dired-omit-region (start end regexp)
  ;; Omits files matching regexp in region. Returns count.
  (save-restriction
    (narrow-to-region start end)
    (let ((hidden-subdirs (dired-remember-hidden))
	  buffer-read-only count)
      (or selective-display
	  (error "selective-display must be t for file omission to work!"))
      (dired-omit-unhide-region start end)
      (let ((dired-marker-char dired-omit-marker-char)
	    ;; since all subdirs are now unhidden, this fakes
	    ;; dired-move-to-end-of-filename into working faster
	    (selective-display nil))
	(or dired-omit-silent
	    dired-in-query (message "Omitting..."))
	(if (dired-mark-unmarked-files regexp nil nil 'no-dir)
	    (setq count (dired-do-hide
			 dired-marker-char
			 (and (memq dired-omit-silent '(nil 0))
			      (not dired-in-query)
			      "Omitted %d line%s.")))
	  (or dired-omit-silent dired-in-query
	      (message "(Nothing to omit)"))))
      (save-excursion		;hide subdirs that were hidden
	(mapcar (function (lambda (dir)
			    (if (dired-goto-subdir dir)
				(dired-hide-subdir 1))))
		hidden-subdirs))
      count)))

(defun dired-omit-unhide-region (beg end)
  ;; Unhides hidden, but not marked files in the region.
  (save-excursion
    (save-restriction
      (narrow-to-region beg end)
      (goto-char (point-min))
      (while (search-forward "\r" nil t)
	(and (char-equal (char-after (point)) ?\ )
	     (subst-char-in-region (1- (point)) (point) ?\r ?\n))))))

(defun dired-do-unhide (char &optional fmt marker)
  ;; Unhides files marked with CHAR. Optional FMT is a message
  ;; to be displayed. Note that after unhiding, we will need to re-hide
  ;; files belonging to hidden subdirs.
  (save-excursion
    (goto-char (point-min))
    (let ((count 0)
	  (string (concat "\r" (char-to-string char)))
	  (hidden-subdirs (dired-remember-hidden))
	  (new (if marker (concat "\n" (char-to-string marker)) "\n "))
	  buffer-read-only)
      (while (search-forward string nil t)
	(replace-match new)
	(setq count (1+ count)))
      (or (equal "" fmt)
	  (message (or fmt "Unhid %d line%s.") count (dired-plural-s count)))
      (goto-char (point-min))
      (mapcar (function (lambda (dir)
			  (if (dired-goto-subdir dir)
			      (dired-hide-subdir 1 t))))
	      hidden-subdirs)
      (if marker (dired-update-mode-line-modified t))
      count)))

(defun dired-do-hide (char &optional fmt)
  ;; Hides files marked with CHAR. Otional FMT is a message
  ;; to be displayed. FMT is a format string taking args the number
  ;; of hidden file lines, and dired-plural-s.
  (save-excursion
    (goto-char (point-min))
    (let ((count 0)
	  (string (concat "\n" (char-to-string char)))
	  buffer-read-only)
      (while (search-forward string nil t)
	(subst-char-in-region (match-beginning 0)
			      (1+ (match-beginning 0)) ?\n ?\r t)
	(setq count (1+ count)))
      (if fmt
	  (message fmt count (dired-plural-s count)))
      count)))

(defun dired-omit-regexp ()
  (let (rgxp)
    (if dired-omit-extensions
	(setq rgxp (concat
		    ".\\("
		    (mapconcat 'regexp-quote dired-omit-extensions "\\|")
		    "\\)\\'")))
    (if dired-omit-regexps
	(setq rgxp
	      (concat
	       rgxp
	       (and rgxp "\\|")
	       (mapconcat 'identity dired-omit-regexps "\\|"))))
    rgxp))

(defun dired-mark-unmarked-files (regexp msg &optional unflag-p localp)
  ;; Marks unmarked files matching REGEXP, displaying MSG.
  ;; REGEXP is matched against the complete pathname, unless localp is
  ;; specified.
  ;; Does not re-mark files which already have a mark.
  ;; Returns t if any work was done, nil otherwise.
  (let ((dired-marker-char (if unflag-p ?\  dired-marker-char))
	fn)
    (dired-mark-if
     (and
      ;; not already marked
      (equal (char-after (point)) ?\ )
      ;; uninteresting
      (setq fn (dired-get-filename localp t))
      (string-match regexp fn))
     msg)))

(defun dired-add-omit-regexp (rgxp &optional how)
  "Adds a new regular expression to the list of omit regular expresions.
With a non-zero numeric prefix argument, deletes a regular expresion from 
the list.

With a prefix argument \\[universal-argument], adds a new extension to
the list of file name extensions omitted.
With a prefix argument \\[universal-argument] \\[universal-argument], deletes
a file name extension from the list.

With a prefix 0, reports on the current omit regular expressions and 
extensions."
  (interactive
   (list
    (cond
     ((null current-prefix-arg)
      (read-string "New omit regular expression: "))
     ((equal '(4) current-prefix-arg)
      (read-string "New omit extension (\".\" is not implicit): "))
     ((equal '(16) current-prefix-arg)
      (completing-read
       "Remove from omit extensions (type SPACE for options): "
       (mapcar 'list dired-omit-extensions) nil t))
     ((eq 0 current-prefix-arg)
      nil)
     (t
      (completing-read
       "Remove from omit regexps (type SPACE for options): "
       (mapcar 'list dired-omit-regexps) nil t)))
    current-prefix-arg))
  (let (remove)
    (cond
     ((null how)
      (if (member rgxp dired-omit-regexps)
	  (progn
	    (describe-variable 'dired-omit-regexps)
	    (error "%s is already included in the list." rgxp))
	(setq dired-omit-regexps (cons rgxp dired-omit-regexps))))
     ((equal how '(4))
      (if (member rgxp dired-omit-extensions)
	  (progn
	    (describe-variable 'dired-omit-extensions)
	    (error "%s is already included in list." rgxp))
	(setq dired-omit-extensions (cons rgxp dired-omit-extensions))))
     ((equal how '(16))
      (let ((tail (member rgxp dired-omit-extensions)))
	(if tail
	    (setq dired-omit-extensions
		  (delq (car tail) dired-omit-extensions)
		  remove t)
	  (setq remove 'ignore))))
     ((eq 0 how)
      (setq remove 'ignore)
      (if (featurep 'ehelp)
	  (with-electric-help
	   (function
	    (lambda ()
	      (princ "Omit extensions (dired-omit-extensions <V>):\n")
	      (dired-format-columns-of-files dired-omit-extensions)
	      (princ "\n")
	      (princ "Omit regular expressions (dired-omit-regexps <V>):\n")
	      (dired-format-columns-of-files dired-omit-regexps)
	      nil)))
	(with-output-to-temp-buffer "*Help*"
	  (princ "Omit extensions (dired-omit-extensions <V>):\n")
	  (dired-format-columns-of-files dired-omit-extensions)
	  (princ "\n")
	  (princ "Omit regular expressions (dired-omit-regexps <V>):\n")
	  (dired-format-columns-of-files dired-omit-regexps)
	  (print-help-return-message))))
     (t
      (let ((tail (member rgxp dired-omit-regexps)))
	(if tail
	    (setq dired-omit-regexps (delq (car tail) dired-omit-regexps)
		  remove t)
	  (setq remove 'ignore)))))
    (or (eq remove 'ignore)
	(save-excursion
	  (mapcar
	   (function
	    (lambda (dir)
	      (if (dired-goto-subdir dir)
		  (progn
		    (if remove
			(save-restriction
			  (narrow-to-region
			   (dired-subdir-min) (dired-subdir-max))
			  (dired-do-unhide dired-omit-marker-char "")))
		    (dired-omit-expunge)))))
	   (dired-remember-omitted))))))



;;;; ----------------------------------------------------------------
;;;; Directory hiding.
;;;; ----------------------------------------------------------------
;;;
;;; To indicate a hidden subdir, we actually insert "..." in the buffer.
;;; Aside from giving the look of ellipses (even though
;;; selective-display-ellipses is nil), it allows us to tell the difference
;;; between a dir with a single omitted file, and a hidden subdir with one
;;; file.

(defun dired-subdir-hidden-p (dir)
  (save-excursion
    (and selective-display
	 (dired-goto-subdir dir)
	 (looking-at "\\.\\.\\.\r"))))

(defun dired-unhide-subdir ()
  (let (buffer-read-only)
    (goto-char (dired-subdir-min))
    (skip-chars-forward "^\n\r")
    (skip-chars-backward "." (- (point) 3))
    (if (looking-at "\\.\\.\\.\r") (delete-char 4))
    (dired-omit-unhide-region (point) (dired-subdir-max))))

(defun dired-hide-check ()
  (or selective-display
      (error "selective-display must be t for subdir hiding to work!")))

(defun dired-hide-subdir (arg &optional really)
  "Hide or unhide the current subdirectory and move to next directory.
Optional prefix arg is a repeat factor.
Use \\[dired-hide-all] to (un)hide all directories.
With the optional argument REALLY, we always hide 
the subdir, regardless of dired-subdir-hidden-p."
  ;; The arg REALLY is needed because when we unhide
  ;; omitted files in a hidden subdir, we want to
  ;; re-hide the subdir, regardless of whether dired
  ;; thinks it's already hidden.
  (interactive "p")
  (dired-hide-check)
  (dired-save-excursion
    (while (>=  (setq arg (1- arg)) 0)
      (let* ((cur-dir (dired-current-directory))
	     (hidden-p (and (null really)
			    (dired-subdir-hidden-p cur-dir)))
	   (elt (assoc cur-dir dired-subdir-alist))
	   (end-pos (1- (dired-get-subdir-max elt)))
	   buffer-read-only)
	;; keep header line visible, hide rest
	(goto-char (dired-get-subdir-min elt))
	(skip-chars-forward "^\n\r")
	(skip-chars-backward "." (- (point) 3))
      (if hidden-p
	  (progn
	    (if (looking-at "\\.\\.\\.\r")
		(progn
		  (delete-char 3)
		  (setq end-pos (- end-pos 3))))
	    (dired-omit-unhide-region (point) end-pos))
	(if (looking-at "\\.\\.\\.\r")
	    (goto-char (match-end 0))
	  (insert "...")
	  (setq end-pos (+ end-pos 3)))
	(subst-char-in-region (point) end-pos ?\n ?\r)))
      (dired-next-subdir 1 t))))
  
(defun dired-hide-all (arg)
  "Hide all subdirectories, leaving only their header lines.
If there is already something hidden, make everything visible again.
Use \\[dired-hide-subdir] to (un)hide a particular subdirectory."
  (interactive "P")
  (dired-hide-check)
  (let (buffer-read-only)
    (dired-save-excursion
      (if (let ((alist dired-subdir-alist)
		(hidden nil))
	    (while (and alist (null hidden))
	      (setq hidden (dired-subdir-hidden-p (car (car alist)))
		    alist (cdr alist)))
	    hidden)
	  ;; unhide
	  (let ((alist dired-subdir-alist))
	    (while alist
	      (goto-char (dired-get-subdir-min (car alist)))
	      (skip-chars-forward "^\n\r")
	      (delete-region (point) (progn (skip-chars-backward ".") (point)))
	      (setq alist (cdr alist)))
	    (dired-omit-unhide-region (point-min) (point-max)))
	;; hide
	(let ((alist dired-subdir-alist))
	  (while alist
	    (dired-goto-subdir (car (car alist)))
	    (dired-hide-subdir 1 t)
	    (setq alist (cdr alist))))))))


;;;; -----------------------------------------------------------------
;;;; Automatic dired buffer maintenance.
;;;; -----------------------------------------------------------------
;;;
;;;  Keeping Dired buffers in sync with the filesystem and with each
;;;  other.
;;;  When used with efs on remote directories, buffer maintainence is
;;;  done asynch.

(defun dired-buffers-for-dir (dir-or-list &optional check-wildcard)
;; Return a list of buffers that dired DIR-OR-LIST
;; (top level or in-situ subdir).
;; The list is in reverse order of buffer creation, most recent last.
;; As a side effect, killed dired buffers for DIR are removed from
;; dired-buffers. If DIR-OR-LIST is a wildcard or list, returns any
;; dired buffers for which DIR-OR-LIST is equal to `dired-directory'.
;; If check-wildcard is non-nil, only returns buffers which contain dir-or-list
;; exactly, including the wildcard part.
  (let ((alist dired-buffers)
	(as-dir (and (stringp dir-or-list)
		     (file-name-as-directory dir-or-list)))
	result buff elt)
    (while alist
      (setq buff (cdr (setq elt (car alist)))
	    alist (cdr alist))
      ;; dired-in-this-tree is not fast. It doesn't pay to use this to check
      ;; whether the buffer is a good candidate.
      (if (buffer-name buff)
	  (save-excursion
	    (set-buffer buff)
	    (if (or (equal dir-or-list dired-directory) ; the wildcard case.
		    (and as-dir
			 (not (and check-wildcard
				   (string-equal
				    as-dir
				    (expand-file-name default-directory))))
			 (assoc as-dir dired-subdir-alist)))
		(setq result (cons buff result))))
	;; else buffer is killed - clean up:
	(setq dired-buffers (delq elt dired-buffers))))
    (or dired-buffers (dired-remove-from-file-name-handler-alist))
    result))

(defun dired-advertise ()
  ;; Advertise in variable `dired-buffers' that we dired `default-directory'.
  ;; With wildcards we actually advertise too much.
  ;; Also makes sure that we are installed in the file-name-handler-alist
  (prog1
      (let ((ddir (expand-file-name default-directory)))
	(if (memq (current-buffer) (dired-buffers-for-dir ddir))
	    t			    ; we have already advertised ourselves
	  (setq dired-buffers
		(cons (cons ddir (current-buffer))
		      dired-buffers))))
    ;; Do this last, otherwise the call to dired-buffers-for-dir will
    ;; remove dired-handler-fn from the file-name-handler-alist.
    ;; Strictly speaking, we only need to do this in th else branch of
    ;; the if statement.  We do it unconditionally as a sanity check.
    (dired-check-file-name-handler-alist)))

(defun dired-unadvertise (dir)
  ;; Remove DIR from the buffer alist in variable dired-buffers.
  ;; This has the effect of removing any buffer whose main directory is DIR.
  ;; It does not affect buffers in which DIR is a subdir.
  ;; Removing is also done as a side-effect in dired-buffer-for-dir.
  (setq dired-buffers
      (delq (assoc dir dired-buffers) dired-buffers))
  ;; If there are no more dired buffers, we are no longer needed in the
  ;; file-name-handler-alist.
  (or dired-buffers (dired-remove-from-file-name-handler-alist)))

(defun dired-unadvertise-current-buffer ()
  ;; Remove all references to the current buffer in dired-buffers.
  (setq dired-buffers
	(delq nil
	      (mapcar
	       (function
		(lambda (x)
		  (and (not (eq (current-buffer) (cdr x))) x)))
	       dired-buffers)))
  ;; If there are no more dired buffers, we are no longer needed in the
  ;; file-name-handler-alist.
  (or dired-buffers (dired-remove-from-file-name-handler-alist)))

(defun dired-fun-in-all-buffers (directory fun &rest args)
  ;; In all buffers dired'ing DIRECTORY, run FUN with ARGS.
  ;; Return list of buffers where FUN succeeded (i.e., returned non-nil).
  (let* ((buf-list (dired-buffers-for-dir directory))
	 (obuf (current-buffer))
	 (owin (selected-window))
	 (win owin)
	 buf windows success-list)
    (if buf-list
	(unwind-protect
	    (progn
	      (while (not (eq (setq win (next-window win)) owin))
		(and (memq (setq buf (window-buffer win)) buf-list)
		     (progn
		       (set-buffer buf)
		       (= (point) (window-point win)))
		     (setq windows (cons win windows))))
	      (while buf-list
		(setq buf (car buf-list)
		      buf-list (cdr buf-list))
		(set-buffer buf)
		(if (apply fun args)
		    (setq success-list (cons (buffer-name buf) success-list))))
	      ;; dired-save-excursion prevents lossage of save-excursion
	      ;; for point.  However, if dired buffers are displayed in
	      ;; other windows, the setting of window-point loses, and
	      ;; drags the point with it.  This should fix this.
	      (while windows
		(condition-case nil
		    (progn
		      (set-buffer (window-buffer (setq win (car windows))))
		      (set-window-point win (point)))
		  (error nil))
		(setq windows (cdr windows))))
	  (set-buffer obuf)))
    success-list))

(defun dired-find-file-place (subdir file)
  ;; Finds a position to insert in SUBDIR FILE. If it can't find SUBDIR,
  ;; returns nil.
  (let ((sort (dired-sort-type dired-internal-switches))
	(rev (memq ?r (nth 3 (assoc subdir dired-subdir-alist)))))
    (cond
     ((eq sort 'name)
      (if (dired-goto-subdir subdir)
	  (let ((max (dired-subdir-max))
		start end found)
	    (if (dired-goto-next-file)
		(progn
		  (skip-chars-forward "^\n\r")
		  (setq start (point))
		  (goto-char (setq end max))
		  (forward-char -1)
		  (skip-chars-backward "^\n\r")
		  ;; This loop must find a file.  At the very least, it will
		  ;; find the one found previously.
		  (while (not found)
		    (if (save-excursion (dired-move-to-filename nil (point)))
			(setq found t)
		      (setq end (point))
		      (forward-char -1)
		      (skip-chars-backward "^\n\r")))
		  (if rev
		      (while (< start end)
			(goto-char (/ (+ start end) 2))
			(if (dired-file-name-lessp
			     (or (dired-get-filename 'no-dir t)
				 (error
				  "Error in dired-find-file-place"))
			     file)
			    (setq end (progn
					(skip-chars-backward "^\n\r")
					(point)))
			  (setq start (progn
					(skip-chars-forward "^\n\r")
					(forward-char 1)
					(skip-chars-forward "^\n\r")
					(point)))))
		    (while (< start end)
		      (goto-char (/ (+ start end) 2))
		      (if (dired-file-name-lessp
			   file
			   (or (dired-get-filename 'no-dir t)
			       (error
				"Error in dired-find-file-place")))
			  (setq end (progn
					(skip-chars-backward "^\n\r")
					(point)))
			(setq start (progn
				      (skip-chars-forward "^\n\r")
				      (forward-char 1)
				      (skip-chars-forward "^\n\r")
				      (point))))))
		  (goto-char end))
	      (goto-char max))
	    t)))
     ((eq sort 'date)
      (if (dired-goto-subdir subdir)
	  (if rev
	      (goto-char (dired-subdir-max))
	    (dired-goto-next-file)
	    t)))
     ;; Put in support for other sorting types.
     (t
      (if (string-equal (dired-current-directory) subdir)
	  (progn
	    ;; We are already where we should be, except when
	    ;; point is before the subdir line or its total line.
	    (or (save-excursion (beginning-of-line) (dired-move-to-filename))
		(dired-goto-next-nontrivial-file)) ; in the header somewhere
	    t) ; return t, for found.
	(if (dired-goto-subdir subdir)
	    (progn
	      (dired-goto-next-nontrivial-file)
	      t)))))))

(defun dired-add-entry (filename &optional marker-char inplace)
  ;; Add a new entry for FILENAME, optionally marking it
  ;; with MARKER-CHAR (a character, else dired-marker-char is used).
  ;; Hidden subdirs are exposed if a file is added there.
  ;; 
  ;; This function now adds the new entry at the END of the previous line,
  ;; not the beginning of the current line.
  ;; Logically, we now think of the `newline' associated
  ;; with a fileline, as the one at the beginning of the line, not the end.
  ;; This makes it easier to keep track of omitted files.
  ;; 
  ;; Uses dired-save-excursion, so that it doesn't move the
  ;; point around. Especially important when it runs asynch.
  ;; 
  ;; If there is already an entry, delete the existing one before adding a
  ;; new one.  In this case, doesn't remember its mark.  Use
  ;; dired-update-file-line for that.
  ;; 
  ;; If INPLACE eq 'relist, then the new entry is put in the
  ;; same place as the old, if there was an old entry.
  ;; If INPLACE is t, then the file entry is put on the line
  ;; currently containing the point.  Otherwise, dired-find-file-place
  ;; attempts to determine where to put the file.

  (setq filename (directory-file-name filename))
  (dired-save-excursion
    (let ((oentry (save-excursion (dired-goto-file filename)))
	  (directory (file-name-directory filename))
	  (file-nodir (file-name-nondirectory filename))
	  buffer-read-only)
      (if oentry
	  ;; Remove old entry
	  (let ((opoint (point)))
	    (goto-char oentry)
	    (delete-region (save-excursion
			     (skip-chars-backward "^\r\n")
			     (dired-update-marker-counters (char-after (point)) t)
			     (1- (point)))
			   (progn
			     (skip-chars-forward "^\r\n")
			     (point)))
	    ;; Move to right place to replace deleted line.
	    (cond ((eq inplace 'relist) (forward-char 1))
		  ((eq inplace t) (goto-char opoint)))
	    (dired-update-mode-line-modified)))
      (if (or (eq inplace t)
	      (and oentry (eq inplace 'relist))
	      ;; Tries to move the point to the right place.
	      ;; Returns t on success.
	      (dired-find-file-place directory file-nodir))
	  (let ((switches (dired-make-switches-string
			   (cons ?d dired-internal-switches)))
		b-of-l)
	    ;; Bind marker-char now, in case we are working asynch and
	    ;; dired-marker-char changes in the meantime.
	    (if (and marker-char (not (characterp marker-char)))
		(setq marker-char dired-marker-char))
	    ;; since we insert at the end of a line,
	    ;; backup to the end of the previous line.
	    (skip-chars-backward "^\n\r")
	    (forward-char -1)
	    (setq b-of-l (point))
	    (if (and (featurep 'efs-dired) efs-dired-host-type)
		;; insert asynch
		;; we call the efs version explicitly here,
		;; rather than let the handler-alist work for us
		;; because we want to pass extra args.
		;; Is there a cleaner way to do this?
		(efs-insert-directory filename ; don't expand `.' !
				      switches nil nil
				      t ; nowait
				      marker-char)
	      (let ((insert-directory-program dired-ls-program))
		(insert-directory filename switches nil nil))
	      (dired-after-add-entry b-of-l marker-char))
	    (if dired-verify-modtimes
		(dired-set-file-modtime directory dired-subdir-alist))
	    t))))) ; return t on success, else nil.

(defun dired-after-add-entry (start marker-char)
  ;; Does the cleanup of a dired entry after listing it.
  ;; START is the start of the new listing-line.
  ;; This is a separate function for the sake of efs.
  (save-excursion
    (goto-char start)
    ;; we make sure that the new line is bracketted by new-lines
    ;; so the user doesn't need to use voodoo in the
    ;; after-readin-hook.
    (insert ?\n)
    (dired-add-entry-do-indentation marker-char)
    (let* ((beg (dired-manual-move-to-filename t))
	   ;; error for strange output
	   (end (dired-manual-move-to-end-of-filename))
	   (filename (buffer-substring beg end)))
      ;; We want to have the non-directory part only.
      (delete-region beg end)
      ;; Any markers pointing to the beginning of the filename, will
      ;; still point there after this insertion. Should keep
      ;; save-excursion from losing.
      (setq beg (point))
      (insert (file-name-nondirectory filename))
      (dired-insert-set-properties beg (point))
      (dired-move-to-filename))
    ;; The subdir-alist is not affected so we can run it right now.
    (let ((omit (dired-current-subdir-omitted-p))
	  (hide (dired-subdir-hidden-p (dired-current-directory))))
      (if (or dired-after-readin-hook omit hide)
	  (save-excursion
	    (save-restriction
	      ;; Use start so that we get the new-line at
	      ;; the beginning of the line in case we want
	      ;; to hide the file. Don't need to test (bobp)
	      ;; here, since we never add a file at
	      ;; the beginning of the buffer.
	      (narrow-to-region start
				(save-excursion (forward-line 1) (point)))
	      (run-hooks 'dired-after-readin-hook)
	      (if omit
		  (let ((dired-omit-silent (or dired-omit-silent 0)))
		    (dired-omit-region (point-min) (point-max)
				       (dired-omit-regexp))))
	      (if hide
		  (subst-char-in-region (point-min) (1- (point-max))
					?\n ?\r))))))
    ;; clobber the extra newline at the end of the line
    (end-of-line)
    (delete-char 1)))

;; This is a separate function for the sake of nested dired format.
(defun dired-add-entry-do-indentation (marker-char)
  ;; two spaces or a marker plus a space:
  (insert (if marker-char
	      (let ((char (if (characterp marker-char)
			      marker-char
			    dired-marker-char)))
		(dired-update-marker-counters char)
		(dired-update-mode-line-modified)
		char)
	    ?\040)
	  ?\040))

(defun dired-remove-file (file)
  (let ((alist dired-buffers)
	buff)
    (save-excursion
      (while alist
	(setq buff (cdr (car alist)))
	(if (buffer-name buff)
	    (progn
	      (set-buffer buff)
	      (dired-remove-entry file))
	  (setq dired-buffers (delq (car alist) dired-buffers)))
	(setq alist (cdr alist))))
    (or dired-buffers (dired-remove-from-file-name-handler-alist))))

(defun dired-remove-entry (file)
  (let ((ddir (expand-file-name default-directory))
	(dirname (file-name-as-directory file)))
    (if (dired-in-this-tree ddir dirname)
	(if (or (memq 'kill-dired-buffer dired-no-confirm)
		(y-or-n-p (format "Kill dired buffer %s for %s, too? "
				  (buffer-name) dired-directory)))
	    (kill-buffer (current-buffer)))
      (if (dired-in-this-tree file ddir)
	  (let ((alist dired-subdir-alist))
	    (while alist
	      (if (dired-in-this-tree (car (car alist)) dirname)
		  (save-excursion
		    (goto-char (dired-get-subdir-min (car alist)))
		    (dired-kill-subdir)))
	      (setq alist (cdr alist)))
	    (dired-save-excursion
	      (and (dired-goto-file file)
		   (let (buffer-read-only)
		     (delete-region
		      (progn (skip-chars-backward "^\n\r")
			     (or (memq (char-after (point)) '(\n \r ?\ ))
				 (progn
				   (dired-update-marker-counters
				    (char-after (point)) t)
				   (dired-update-mode-line-modified)))
			     (1- (point)))
		      (progn (skip-chars-forward "^\n\r") (point)))
		     (if dired-verify-modtimes
			 (dired-set-file-modtime
			  (file-name-directory (directory-file-name file))
			  dired-subdir-alist))))))))))

(defun dired-add-file (filename &optional marker-char)
  (dired-fun-in-all-buffers
   (file-name-directory filename)
   (function dired-add-entry) filename marker-char))

(defun dired-relist-file (file)
  (dired-uncache file nil)
  (dired-fun-in-all-buffers (file-name-directory file)
			    (function dired-relist-entry) file))

(defun dired-relist-entry (file)
  ;; Relist the line for FILE, or just add it if it did not exist.
  ;; FILE must be an absolute pathname.
  (let* ((file (directory-file-name file))
	 (directory (file-name-directory file))
	 (dd (expand-file-name default-directory)))
    (if (assoc directory dired-subdir-alist)
	(if (or
	     ;; Not a wildcard
	     (equal dd dired-directory)
	     ;; Not top-level
	     (not (string-equal directory dd))
	     (and (string-equal directory
				(if (consp dired-directory)
				    (file-name-as-directory
				     (car dired-directory))
				  (file-name-directory dired-directory)))
		  (dired-file-in-wildcard-p dired-directory file)))
	    (let ((marker (save-excursion
			    (and (dired-goto-file file)
				 (dired-file-marker file)))))
	      ;; recompute omission
	      (if (equal marker dired-omit-marker-char)
		  (setq marker nil))
	      (dired-add-entry file marker 'relist))
	  ;; At least tell dired that we considered updating the buffer.
	  (if dired-verify-modtimes
	      (dired-set-file-modtime directory dired-subdir-alist))))))

(defun dired-file-in-wildcard-p (wildcard file)
  ;; Return t if a file is part of the listing for wildcard.
  ;; File should be the non-directory part only.
  ;; This version is slow, but meticulously correct.  Is it worth it?
  (if (consp wildcard)
      (let ((files (cdr wildcard))
	    (dir (car wildcard))
	    yep)
	(while (and files (not yep))
	  (setq yep (string-equal file (expand-file-name (car files) dir))
		files (cdr files)))
	yep)
    (let ((err-buff
	   (let ((default-major-mode 'fundamental-mode))
	     (get-buffer-create " *dired-check-process output*")))
	  (dir default-directory)
	  (process-connection-type nil))
      (save-excursion
	(set-buffer err-buff)
	(erase-buffer)
	(setq default-directory dir)
	(call-process shell-file-name nil t nil shell-command-switch
		    (concat dired-ls-program " -d " wildcard " | "
			    "egrep '(^|/)" file "$'"))
	(/= (buffer-size) 0)))))

;; The difference between dired-add-file and dired-relist-file is that
;; the former creates the entry with a specific marker.  The later preserves
;; existing markers on a per buffer basis.  This is not the same as
;; giving dired-create-files a marker of t, which uses a marker in a specific
;; buffer to determine the marker for file line creation in all buffers.


;;;; ----------------------------------------------------------------
;;;; Applying Lisp functions to marked files.
;;;; ----------------------------------------------------------------

;;; Running tags commands on marked files.
;;
;; Written 8/30/93 by Roland McGrath <roland@gnu.ai.mit.edu>.
;; Requires tags.el as distributed with GNU Emacs 19.23, or later.

(defun dired-do-tags-search (regexp)
  "Search through all marked files for a match for REGEXP.
Stops when a match is found.
To continue searching for next match, use command \\[tags-loop-continue]."
  (interactive "sSearch marked files (regexp): ")
  (tags-search regexp '(dired-get-marked-files)))

(defun dired-do-tags-query-replace (from to &optional delimited)
  "Query-replace-regexp FROM with TO through all marked files.
Third arg DELIMITED (prefix arg) means replace only word-delimited matches.
If you exit (\\[keyboard-quit] or ESC), you can resume the query-replace
with the command \\[tags-loop-continue]."
  (interactive
   "sQuery replace in marked files (regexp): \nsQuery replace %s by: \nP")
  (tags-query-replace from to delimited '(dired-get-marked-files)))

;;; byte compiling

(defun dired-byte-compile ()
  ;; Return nil for success, offending file name else.
  (let* ((filename (dired-get-filename))
	 buffer-read-only failure)
    (condition-case err
	(save-excursion (byte-compile-file filename))
      (error
       (setq failure err)))
    ;; We should not need to update any file lines, as this will have
    ;; already been done by after-write-region-hook.
    (and failure
	 (progn
	   (dired-log (buffer-name (current-buffer))
		      "Byte compile error for %s:\n%s\n" filename failure)
	   (dired-make-relative filename)))))

(defun dired-do-byte-compile (&optional arg)
  "Byte compile marked (or next ARG) Emacs lisp files."
  (interactive "P")
  (dired-map-over-marks-check (function dired-byte-compile) arg
			      'byte-compile "byte-compile" t))

;;; loading

(defun dired-load ()
  ;; Return nil for success, offending file name else.
  (let ((file (dired-get-filename)) failure)
    (condition-case err
      (load file nil nil t)
      (error (setq failure err)))
    (if (not failure)
	nil
      (dired-log (buffer-name (current-buffer))
		 "Load error for %s:\n%s\n" file failure)
      (dired-make-relative file))))

(defun dired-do-load (&optional arg)
  "Load the marked (or next ARG) Emacs lisp files."
  (interactive "P")
  (dired-map-over-marks-check (function dired-load) arg 'load "load" t))


;;;; ----------------------------------------------------------------
;;;; File Name Handler Alist
;;;; ----------------------------------------------------------------
;;;
;;;  Make sure that I/O functions maintain dired buffers.

(defun dired-remove-from-file-name-handler-alist ()
  ;; Remove dired from the file-name-handler-alist
  (setq file-name-handler-alist
	(delq nil
	      (mapcar
	       (function
		(lambda (x)
		  (and (not (eq (cdr x) 'dired-handler-fn))
		       x)))
	       file-name-handler-alist))))

(defun dired-check-file-name-handler-alist ()
  ;; Verify that dired is installed as the first item in the alist
  (and dired-refresh-automatically
       (or (eq (cdr (car file-name-handler-alist)) 'dired-handler-fn)
	   (setq file-name-handler-alist
		 (cons
		  '("." . dired-handler-fn)
		  (dired-remove-from-file-name-handler-alist))))))

(defun dired-handler-fn (op &rest args)
  ;; Function to update dired buffers after I/O.
  (prog1
      (let ((inhibit-file-name-handlers
	     (cons 'dired-handler-fn
		   (and (eq inhibit-file-name-operation op)
			inhibit-file-name-handlers)))
	    (inhibit-file-name-operation op))
	(apply op args))
    (let ((dired-omit-silent t)
	  (hf (get op 'dired)))
      (and hf (funcall hf args)))))

(defun dired-handler-fn-1 (args)
  (let ((to (expand-file-name (nth 1 args))))
    (or (member to dired-unhandle-add-files)
	(dired-relist-file to))))

(defun dired-handler-fn-2 (args)
  (let ((from (expand-file-name (car args)))
	(to (expand-file-name (nth 1 args))))
    ;; Don't remove the original entry if making backups.
    ;; Otherwise we lose marks.  I'm not completely happy with the
    ;; logic here.
    (or (and
	 (eq (nth 2 args) t) ; backups always have OK-IF-OVERWRITE t
	 (string-equal (car (find-backup-file-name from)) to))
	(dired-remove-file from))
    (or (member to dired-unhandle-add-files)
	(dired-relist-file to))))

(defun dired-handler-fn-3 (args)
  (let ((to (expand-file-name (nth 2 args))))
    (or (member to dired-unhandle-add-files)
	(dired-relist-file to))))

(defun dired-handler-fn-4 (args)
  (dired-remove-file (expand-file-name (car args))))

(defun dired-handler-fn-5 (args)
  (let ((to (expand-file-name (car args))))
    (or (member to dired-unhandle-add-files)
	(dired-relist-file to))))

(defun dired-handler-fn-6 (args)
  (let ((to (expand-file-name (nth 1 args)))
	(old (expand-file-name (car args))))
    (or (member to dired-unhandle-add-files)
	(dired-relist-file to))
    (dired-relist-file old)))

(put 'copy-file 'dired 'dired-handler-fn-1)
(put 'dired-make-relative-symlink 'dired 'dired-handler-fn-1)
(put 'make-symbolic-link 'dired 'dired-handler-fn-1)
(put 'add-name-to-file 'dired 'dired-handler-fn-6)
(put 'rename-file 'dired 'dired-handler-fn-2)
(put 'write-region 'dired 'dired-handler-fn-3)
(put 'delete-file 'dired 'dired-handler-fn-4)
(put 'delete-directory 'dired 'dired-handler-fn-4)
(put 'dired-recursive-delete-directory 'dired 'dired-handler-fn-4)
(put 'make-directory-internal 'dired 'dired-handler-fn-5)
(put 'set-file-modes 'dired 'dired-handler-fn-5)
  
;;;; --------------------------------------------------------------
;;;; Multi-flavour Emacs support
;;;; --------------------------------------------------------------

(let ((lucid-p (string-match "XEmacs" emacs-version))
      ver)
  (or (string-match "^\\([0-9]+\\)\\." emacs-version)
      (error "Weird emacs version %s" emacs-version))
  (setq ver (string-to-int (substring emacs-version (match-beginning 1)
				      (match-end 1))))

  ;; Reading with history.
  (if (>= ver 19)

      (defun dired-read-with-history (prompt initial history)
	(read-from-minibuffer prompt initial nil nil history))
    
    (defun dired-read-with-history (prompt initial history)
      (let ((minibuffer-history-symbol history)) ; for gmhist
	(read-string prompt initial))))
  
  ;; Completing read with history.
  (if (>= ver 19)

      (fset 'dired-completing-read 'completing-read)

    (defun dired-completing-read (prompt table &optional predicate
					 require-match initial-input history)
      (let ((minibuffer-history-symbol history)) ; for gmhist
	(completing-read prompt table predicate require-match
			 initial-input))))
  
  ;; Abbreviating file names.
  (if lucid-p
      (fset 'dired-abbreviate-file-name
	    ;; Lemacs has this extra hack-homedir arg
	    (function
	     (lambda (fn)
	       (abbreviate-file-name fn t))))
    (fset 'dired-abbreviate-file-name 'abbreviate-file-name))

  ;; Deleting directories
  ;; Check for pre 19.8 versions of lucid emacs.
  (if lucid-p
      (or (fboundp 'delete-directory)
	  (fset 'delete-directory 'remove-directory)))
  
  ;; Minibuffers
  (if (= ver 18)
    
      (defun dired-get-active-minibuffer-window ()
	(and (> (minibuffer-depth) 0)
	     (minibuffer-window)))
    
    (defun dired-get-active-minibuffer-window ()
      (let ((frames (frame-list))
	    win found)
	(while frames
	  (if (and (setq win (minibuffer-window (car frames)))
		   (minibuffer-window-active-p win))
	      (setq found win
		    frames nil)
	    (setq frames (cdr frames))))
	found)))

  ;; Text properties and menus.

  (cond
   (lucid-p
    (require 'dired-xemacs))
   ((>= ver 19)
    (require 'dired-fsf))
   (t
    ;; text property stuff doesn't work in V18
    (fset 'dired-insert-set-properties 'ignore)
    (fset 'dired-remove-text-properties 'ignore)
    (fset 'dired-set-text-properties 'ignore)
    (fset 'dired-move-to-filename 'dired-manual-move-to-filename)
    (fset 'dired-move-to-end-of-filename
	  'dired-manual-move-to-end-of-filename))))

;;; MULE

(if (or (boundp 'MULE) (featurep 'mule)) (load "dired-mule"))


;; Run load hook for user customization.
(run-hooks 'dired-load-hook)

;;; end of dired.el