File: efs-dired.el

package info (click to toggle)
xemacs20 20.4-13
  • links: PTS
  • area: main
  • in suites: slink
  • size: 67,324 kB
  • ctags: 57,643
  • sloc: lisp: 586,197; ansic: 184,662; sh: 4,296; asm: 3,179; makefile: 2,021; perl: 1,059; csh: 96; sed: 22
file content (1635 lines) | stat: -rw-r--r-- 56,983 bytes parent folder | download | duplicates (3)
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
;; -*-Emacs-Lisp-*-
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; File:         efs-dired.el
;; Release:      $efs release: 1.15 $
;; Version:      #Revision: 1.32 $
;; RCS:          
;; Description:  Extends much of Dired to work under efs.
;; Authors:      Sebastian Kremer <sk@thp.uni-koeln.de>, 
;;               Andy Norman <ange@hplb.hpl.hp.com>,
;;               Sandy Rutherford <sandy@ibm550.sissa.it>
;; Created:      Throughout the ages.
;; Language:     Emacs-Lisp
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; Provisions and requirements

(provide 'efs-dired)
(require 'efs)
(require 'dired)
(autoload 'dired-shell-call-process "dired-shell")

(defconst efs-dired-version
  (concat (substring "$efs release: 1.15 $" 14 -2)
	  "/"
	  (substring "#Revision: 1.32 $" 11 -2)))

;;;; ----------------------------------------------------------------
;;;; User Configuration Variables
;;;; ----------------------------------------------------------------

(defvar efs-dired-verify-modtime-host-regexp nil
  "Regular expression determining on which hosts dired modtimes are checked.")

(defvar efs-dired-verify-anonymous-modtime nil
  "If non-nil, dired modtimes are checked for anonymous logins.")

;;; Internal Variables

(make-variable-buffer-local 'dired-ls-F-marks-symlinks)

;;;; -----------------------------------------------------------
;;;; Inserting Directories into Buffers
;;;; -----------------------------------------------------------

;; The main command for inserting a directory listing in a buffer.
;; In Emacs 19 this is in files.el, and not specifically connected to
;; dired. Since our version of it uses some dired functions, it is
;; included here, but there is an autoload for it in efs.el.

(defun efs-insert-directory (file switches &optional wildcard full-directory-p
				  nowait marker-char)
  ;; Inserts a remote directory. Can do this asynch.
  (let* ((parsed (efs-ftp-path file))
	 (mk (point-marker))
	 (host (car parsed))
	 (user (nth 1 parsed))
	 (path (nth 2 parsed))
	 (host-type (efs-host-type host))
	 (dumb (memq host-type efs-dumb-host-types))
	 (subdir (and (null (or full-directory-p wildcard))
		      (condition-case nil
			  (dired-current-directory)
			(error nil))))
	 (case-fold-search nil) ; for testing switches
	 (parse (and full-directory-p (not wildcard)
		     (or dumb (efs-parsable-switches-p switches))))
	 ;; In case dired-omit-silent isn't defined.
	 (dired-omit-silent (and (boundp 'dired-omit-silent)
				 dired-omit-silent)))
    
    ;; Insert the listing. If it's not a wild-card, and not a full-dir,
    ;; then we are updating a dired-line. Do this asynch.
    ;; This way of doing the listing makes sure that the dired
    ;; buffer is still around after the listing is obtained.
    
    (efs-ls
     file switches t (if parse 'parse t) nil
     ;; asynch, if we're inserting in a subdir. Do it nowait = 0, so
     ;; updating the file line gets a high priority??
     ;; Insert subdir listings NOWAIT = 0 also so 1-line
     ;; updates don't toggle the mode line.
     (if (and subdir nowait) 0 nowait)
     (efs-cont (listing) (host user file path wildcard
			       nowait marker-char
			       mk subdir parse switches dired-omit-silent)
       ;; We pass the value of dired-omit-silent from the caller to the cont.
       (let ((host-type (efs-host-type host))
	     (listing-type (efs-listing-type host user)))
	 (if (marker-buffer mk)
	     (efs-save-buffer-excursion
	       (set-buffer (marker-buffer mk))
	       ;; parsing a listing, sometimes updates info
	       (if (and parse (eq major-mode 'dired-mode))
		   (progn
		     (setq efs-dired-host-type host-type
			   efs-dired-listing-type listing-type
			   efs-dired-listing-type-string
			   (and efs-show-host-type-in-dired
				(concat " "
					(symbol-name
					 efs-dired-listing-type))))
		     (if (memq host-type '(bsd-unix next-unix))
			 (setq dired-ls-F-marks-symlinks nil)
		       (if (memq host-type '(sysV-unix apollo-unix))
			   (setq dired-ls-F-marks-symlinks t)))))
	       (if subdir
		   ;; a 1-line re-list
		   (save-excursion
		     (efs-update-file-info
		      host-type file efs-data-buffer-name)
		     (goto-char mk)
		     (let ((new-subdir (condition-case nil
					   (dired-current-directory)
					 (error nil)))
			   buffer-read-only)
		       (if (and new-subdir
				(string-equal subdir new-subdir))
			   (progn
			     ;; Is there an existing entry?
			     (if (dired-goto-file file)
				 (progn
				   (delete-region
				    (save-excursion
				      (skip-chars-backward "^\n\r")
				      (1- (point)))
				    (progn
				      (skip-chars-forward "^\n\r")
				      (point)))
				   (goto-char mk)))
			     (insert listing)
			     (save-restriction
			       (narrow-to-region mk (point))
			       (efs-dired-fixup-listing
				listing-type file path switches wildcard)
			       (efs-dired-ls-trim
				listing-type)
			       ;; save-excursion loses if fixup had to
			       ;; remove and re-add the region. Say for
			       ;; sorting.
			       (goto-char (point-max)))
			     (if (and nowait (eq major-mode 'dired-mode))
				 (dired-after-add-entry
				  (marker-position mk)
				  marker-char))))))
		 (goto-char mk)
		 (let (buffer-read-only)
		   (insert listing)
		   (save-restriction
		     (narrow-to-region mk (point))
		     (efs-dired-fixup-listing
		      listing-type file path switches wildcard)
		     (goto-char (point-max))))))))))
     ;; Return 0 if synch, nil if asynch
    (if nowait nil 0)))

;;; Functions for cleaning listings.

(efs-defun efs-dired-ls-trim nil ()
  ;; Trims dir listings, so that the listing of a single file is one line.
  nil)

(efs-defun efs-dired-fixup-listing nil (file path &optional switches wildcard)
  ;; FILE is in efs syntax.
  ;; PATH is just the remote path.
  ;; Some ftpd's put the whole directory name in front of each filename.
  ;; Seems to depend in a strange way on server-client interaction.
  ;; Walk down the listing generated and remove this stuff.
  ;; SWITCHES is a string.
  (if (memq efs-key efs-unix-host-types)
      (let ((continue t)
	    spot bol)
	(goto-char (point-min))
	(while (and (not (eobp)) continue)
	  (and (setq bol (point)
		     spot (dired-manual-move-to-filename nil bol))
	       (setq continue (= (following-char) ?/))
	       (dired-manual-move-to-end-of-filename t bol)
	       (progn
		 (skip-chars-backward "^/")
		 (delete-region spot (point))))
	  (forward-line 1))
	(efs-save-match-data
	  (if (and switches (string-match "R" switches)
		   (not (string-match "d" switches)))
	      (let ((subdir-regexp "^\\(/[^ \n\r]+\\):[\n\r]")
		    name)
		(goto-char (point-min))
		(while (re-search-forward subdir-regexp nil t)
		  (goto-char (match-beginning 0))
		  ;; There may be /./ type nonsense.
		  ;; expand-file-name will handle it.
		  (setq name (expand-file-name
			      (buffer-substring (point) (match-end 0))))
		  (delete-region (point) (match-end 0))
		  (insert (efs-replace-path-component file name)))))))))


;;;; ------------------------------------------------------------
;;;; Tree Dired support
;;;; ------------------------------------------------------------

;;; efs-dired keymap

(defvar efs-dired-map nil
  "Keymap for efs commands in dired buffers.")

(if efs-dired-map
    ()
  (setq efs-dired-map (make-sparse-keymap))
  (define-key efs-dired-map "c" 'efs-dired-close-ftp-process)
  (define-key efs-dired-map "k" 'efs-dired-kill-ftp-process)
  (define-key efs-dired-map "o" 'efs-dired-display-ftp-process-buffer)
  (define-key efs-dired-map "p" 'efs-dired-ping-connection))

(fset 'efs-dired-prefix efs-dired-map)

;;; Functions for dealing with the FTP process

(defun efs-dired-close-ftp-process ()
  "Close the FTP process for the current dired buffer.
Closing causes the connection to be dropped, but efs will retain its
cached data for the connection.  This will make it more efficient to
reopen the connection."
  (interactive)
  (or efs-dired-host-type
      (error "Dired buffer is not for a remote directory."))
  (efs-close-ftp-process (current-buffer))
  (let ((parsed (efs-ftp-path default-directory)))
    (message "Closed FTP connection for %s@%s." (nth 1 parsed) (car parsed))))

(defun efs-dired-kill-ftp-process ()
  "Kills the FTP process for the current dired buffer.
Killing causes the connection to be closed, the process buffer to be killed,
and most of efs's cached data to be wiped."
  (interactive)
  (or efs-dired-host-type
      (error "Dired buffer is not for a remote directory."))
  (efs-kill-ftp-process (current-buffer))
  (let ((parsed (efs-ftp-path default-directory)))
    (message "Killed FTP connection for %s@%s." (nth 1 parsed) (car parsed))))

(defun efs-dired-display-ftp-process-buffer ()
  "Displays in another window the FTP process buffer for a dired buffer."
  (interactive)
  (or efs-dired-host-type
      (error "Dired buffer is not for a remote directory."))
  (efs-display-ftp-process-buffer (current-buffer)))

(defun efs-dired-ping-connection ()
  "Pings FTP connection associated with current dired buffer."
  (interactive)
  (or efs-dired-host-type
      (error "Dired buffer is not for a remote directory."))
  (efs-ping-ftp-connection (current-buffer)))


;;; Reading in dired buffers.

(defun efs-dired-revert (&optional arg noconfirm)
  (let ((efs-ls-uncache t))
    (dired-revert arg noconfirm)))

(defun efs-dired-default-dir-function ()
  (let* ((cd (dired-current-directory))
	 (parsed (efs-ftp-path cd)))
    (if parsed
	(efs-save-match-data
	  (let ((tail directory-abbrev-alist))
	    (while tail
	      (if (string-match (car (car tail)) cd)
		  (setq cd (concat (cdr (car tail))
				   (substring cd (match-end 0)))
			parsed nil))
	      (setq tail (cdr tail)))
	    (apply 'efs-unexpand-parsed-filename
		   (or parsed (efs-ftp-path cd)))))
      cd)))

(defun efs-dired-before-readin ()
  ;; Put in the dired-before-readin-hook.
  (let ((parsed (efs-ftp-path default-directory)))
    (if parsed
	(let ((host (car parsed))
	      (user (nth 1 parsed)))
	  (setq efs-dired-listing-type (efs-listing-type host user)
		efs-dired-host-type (efs-host-type host)
		efs-dired-listing-type-string
		(and efs-show-host-type-in-dired
		     (concat " " (symbol-name efs-dired-listing-type))))
	  (set (make-local-variable 'revert-buffer-function)
	       (function efs-dired-revert))
	  (set (make-local-variable 'default-directory-function)
	       (function efs-dired-default-dir-function))
	  (set (make-local-variable 'dired-verify-modtimes)
	       (null (null (and
			    efs-dired-verify-modtime-host-regexp
			    (efs-save-match-data
			      (let ((case-fold-search t))
				(string-match
				 efs-dired-verify-modtime-host-regexp host))
				  (or efs-dired-verify-anonymous-modtime
				      (not (efs-anonymous-p user))))))))
	  ;; The hellsoft ftp server mixes up cases.
	  ;; However, we may not be able to catch this until
	  ;; after the first directory is listed. 
	  (if (and
	       (eq efs-dired-host-type 'hell)
	       (not (string-equal default-directory
				  (setq default-directory
					(downcase default-directory)))))
	      (or (string-equal (buffer-name) (downcase (buffer-name)))
		  (rename-buffer (generate-new-buffer-name
				  (directory-file-name default-directory)))))
	  ;; Setup the executable and directory regexps
	  (let ((eentry (assq efs-dired-listing-type
			      efs-dired-re-exe-alist))
		(dentry (assq efs-dired-listing-type
			      efs-dired-re-dir-alist)))
	    (if eentry
		(set (make-local-variable 'dired-re-exe) (cdr eentry)))
	    (if dentry
		(set (make-local-variable 'dired-re-dir) (cdr dentry))))
	  ;; No switches are sent to dumb hosts, so don't confuse dired.
	  ;; I hope that dired doesn't get excited if it doesn't see the l
	  ;; switch. If it does, then maybe fake things by setting this to
	  ;; "-Al".
	  (if (eq efs-dired-listing-type 'vms)
	      (setq dired-internal-switches
		    (delq ?F dired-internal-switches))
	    (if (memq efs-dired-host-type efs-dumb-host-types)
		(setq dired-internal-switches '(?l ?A)
		      ;; Don't lie on the mode line
		      dired-sort-mode "")))
	  ;; If the remote file system is version-based, don't set
	  ;; dired-kept-versions to 0. It will flag the most recent
	  ;; copy of the file for deletion -- this isn't really a backup.
	  (if (memq efs-dired-host-type efs-version-host-types)
	      (set (make-local-variable 'dired-kept-versions)
		   (max 1 dired-kept-versions)))))))

(efs-defun efs-dired-insert-headerline (&use efs-dired-listing-type) (dir)
  "Documented as original."
  (efs-real-dired-insert-headerline dir))

(defun efs-dired-uncache (file dir-p)
  ;; Remove FILE from cache.
  (if dir-p
      (efs-del-from-ls-cache file nil t)
    (efs-del-from-ls-cache file t nil)))

;;; Checking modtimes of directories.
;;
;;  This only runs if efs-dired-verify-anonymous-modtime and
;;  efs-verify-modtime-host-regexp turn it on.  Few (any?) FTP servers
;;  support getting MDTM for directories.  As usual, we cache whether
;;  this works, and don't keep senselessly trying it if it doesn't.

(defun efs-dired-file-modtime (file)
  ;; Returns the modtime.
  (let* ((parsed (efs-ftp-path file))
	 (host (car parsed))
	 (user (nth 1 parsed))
	 (rpath (nth 2 parsed)))
    (and (null (efs-get-host-property host 'dir-mdtm-failed))
	 (let ((result (efs-send-cmd host user (list 'quote 'mdtm rpath)
				     (and (eq efs-verbose t)
					  "Getting modtime")))
	       mp)
	   (if (and (null (car result))
		    (setq mp (efs-parse-mdtime (nth 1 result))))
	       (let ((ent (efs-get-file-entry file)))
		 (if ent
		     (setcdr ent (list (nth 1 ent) (nth 2 ent)
				       (nth 3 ent) (nth 4 ent) mp)))
		 parsed)
	     (efs-set-host-property host 'dir-mdtm-failed t)
	     nil)))))

(defun efs-dired-set-file-modtime (file alist)
  ;; This works asynch.
  (let* ((parsed (efs-ftp-path file))
	 (host (car parsed))
	 (user (nth 1 parsed))
	 (path (nth 2 parsed)))
    (if (efs-get-host-property host 'dir-mdtm-failed)
	(let ((elt (assoc file alist)))
	  (if elt (setcar (nthcdr 4 elt) nil)))
      (efs-send-cmd
       host user (list 'quote 'mdtm path) nil nil
       (efs-cont (result line cont-lines) (file alist host)
	 (let ((elt (assoc file alist))
	       modtime)
	   (if (and (null result) (setq modtime (efs-parse-mdtime line)))
	       (if elt (setcar (nthcdr 4 elt) modtime))
	     (if elt (setcar (nthcdr 4 elt) nil))
	     (efs-set-host-property host 'dir-mdtm-failed t))))
       0)    ; Always do this NOWAIT = 0
      nil))) ; return NIL

;;; Asynch insertion of subdirs.  Used when renaming subdirs.

(defun efs-dired-insert-subdir (dirname &optional noerror nowait)
  (let ((buff (current-buffer))
	(switches (delq ?R (copy-sequence dired-internal-switches))))
    (efs-ls
     dirname (dired-make-switches-string switches)
     t nil noerror nowait
     (efs-cont (listing) (dirname buff switches)
       (if (and listing (get-buffer buff))
	   (save-excursion
	     (set-buffer buff)
	     (save-excursion
	       (let ((elt (assoc dirname dired-subdir-alist))
		     mark-list)
		 (if elt
		     (setq mark-list (dired-insert-subdir-del elt))
		   (dired-insert-subdir-newpos dirname))
		 (dired-insert-subdir-doupdate
		  dirname
		  (efs-dired-insert-subdir-do-insert dirname listing)
		  switches elt mark-list)))))))))

(defun efs-dired-insert-subdir-do-insert (dirname listing)
  (let ((begin (point))
	indent-tabs-mode end)
    (insert listing)
    (setq end (point-marker))
    (indent-rigidly begin end 2)
    (goto-char begin)
    (dired-insert-headerline dirname)
    ;; If the listing has null lines `quote' them so that "\n\n" delimits
    ;; subdirs.  This is OK, because we aren't inserting -R listings.
    (save-excursion
      (while (search-forward "\n\n" end t)
	(forward-char -1)
	(insert " ")))
    ;; point is now like in dired-build-subdir-alist
    (prog1
	(list begin (marker-position end))
      (set-marker end nil))))

;;; Moving around in dired buffers.

(efs-defun efs-dired-manual-move-to-filename (&use efs-dired-listing-type)
  (&optional raise-error bol eol)
  "Documented as original."
  (efs-real-dired-manual-move-to-filename raise-error bol eol))

(efs-defun efs-dired-manual-move-to-end-of-filename
  (&use efs-dired-listing-type) (&optional no-error bol eol)
  "Documented as original."
  (efs-real-dired-manual-move-to-end-of-filename no-error bol eol))

(efs-defun efs-dired-make-filename-string (&use efs-dired-listing-type)
  (filename &optional reverse)
  "Documented as original."
  ;; This translates file names from the way that they are displayed
  ;; in listings to the way that the user gives them in the minibuffer.
  ;; For example, in CMS this should take "FOO BAR" to "FOO.BAR".
  filename)

(defun efs-dired-find-file ()
  "Documented as original."
  (interactive)
  (find-file
   (if (memq efs-dired-host-type efs-version-host-types)
       (efs-internal-file-name-sans-versions
	efs-dired-host-type (dired-get-filename) t)
     (dired-get-filename))))

(defun efs-dired-find-file-other-window (&optional display)
  "Documented as original."
  (interactive "P")
  (if display
      (dired-display-file)
    (let ((file (dired-get-filename)))
      (if (memq efs-dired-host-type efs-version-host-types)
	  (setq file (efs-internal-file-name-sans-versions
		      efs-dired-host-type file t)))
      (find-file-other-window file))))

(defun efs-dired-display-file ()
  "Documented as original."
  (interactive)
  (let ((file (dired-get-filename)))
    (if (memq efs-dired-host-type efs-version-host-types)
	(setq file (efs-internal-file-name-sans-versions
		    efs-dired-host-type file t)))
    (display-buffer (find-file-noselect file))))

(defun efs-dired-find-file-other-frame ()
  "Documented as original."
  (interactive)
  (find-file-other-frame
   (if (memq efs-dired-host-type efs-version-host-types)
       (efs-internal-file-name-sans-versions
	efs-dired-host-type (dired-get-filename) t)
     (dired-get-filename))))

;;; Creating and deleting new directories.

(defun efs-dired-recursive-delete-directory (fn)
  ;; Does recursive deletion of remote directories for dired.
  (or (file-exists-p fn)
      (signal 'file-error
	      (list "Removing old file name" "no such directory" fn)))
  (efs-dired-internal-recursive-delete-directory fn))

(defun efs-dired-internal-recursive-delete-directory (fn)
  (if (eq (car (file-attributes fn)) t)
      (let ((files (efs-directory-files fn)))
	(if files
	    (mapcar (function
		     (lambda (ent)
		       (or (string-equal "." ent)
			   (string-equal ".." ent)
			   (efs-dired-internal-recursive-delete-directory
			    (expand-file-name ent fn)))))
		    files))
	(efs-delete-directory fn))
    (condition-case err
	(efs-delete-file fn)
      (ftp-error (if (and (nth 2 err) (stringp (nth 2 err))
			  (efs-save-match-data
			    (string-match "^FTP Error: \"550 " (nth 2 err))))
		     (message "File %s already deleted." fn)
		   (signal (car err) (cdr err)))))))

;;; File backups and versions.

(efs-defun efs-dired-flag-backup-files
  (&use efs-dired-host-type) (&optional unflag-p)
  "Documented as original."
  (interactive "P")
  (efs-real-dired-flag-backup-files unflag-p))

(efs-defun efs-dired-collect-file-versions (&use efs-dired-host-type) ()
  ;; If it looks like a file has versions, return a list of the versions.
  ;; The return value is ((FILENAME . (VERSION1 VERSION2 ...)) ...)
  (efs-real-dired-collect-file-versions))

;;; Sorting dired buffers

(defun efs-dired-file-name-lessp (name1 name2)
  (if (and efs-dired-host-type
	   (memq efs-dired-host-type efs-case-insensitive-host-types))
      (string< (downcase name1) (downcase name2))
    (string< name1 name2)))

;;; Support for async file creators.

(defun efs-dired-copy-file (from to ok-flag &optional cont nowait)
  ;; Version of dired-copy-file for remote files.
  ;; Assumes that filenames are already expanded.
  (dired-handle-overwrite to)
  (efs-copy-file-internal from (efs-ftp-path from) to (efs-ftp-path to)
			  ok-flag dired-copy-preserve-time 0 cont nowait))

(defun efs-dired-rename-file (from to ok-flag &optional cont nowait
				   insert-subdir)
  ;; Version of dired-rename-file for remote files.
  (dired-handle-overwrite to)
  (efs-rename-file-internal
   from to ok-flag nil
   (efs-cont (result line cont-lines) (from to cont insert-subdir)
     (if result
	 (if cont
	     (efs-call-cont cont result line cont-lines)
	   (signal 'ftp-error
		   (list "Dired Renaming"
			 (format "FTP Error: \"%s\"" line)
			 from to)))
       (dired-remove-file from)
       ;; Silently rename the visited file of any buffer visiting this file.
       ;; We do not maintain inserted subdirs for remote 
       (efs-dired-rename-update-buffers from to insert-subdir)
       (if cont (efs-call-cont cont result line cont-lines))))
   nowait))

(defun efs-dired-rename-update-buffers (from to &optional insert-subdir)
  (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-subdir
		   (assoc (file-name-directory (directory-file-name to))
			  dired-subdir-alist)
		   (if (efs-ftp-path to)
		       (efs-dired-insert-subdir to t 1)
		     (dired-insert-subdir to)))))))))))

(defun efs-dired-make-relative-symlink (from to ok-flag &optional cont nowait)
  ;; efs version of dired-make-relative-symlink
  ;; Called as a file-name-handler when dired-make-relative-symlink is
  ;; called interactively.
  ;; efs-dired-create-files calls it directly to supply CONT
  ;; and NOWAIT args.
  (setq from (directory-file-name from)
	to (directory-file-name to))
  (efs-make-symbolic-link-internal
   (dired-make-relative from (file-name-directory to) t)
   to ok-flag cont nowait))

(defun efs-dired-create-files (file-creator operation fn-list name-constructor
					    &optional marker-char query
					    implicit-to)
  "Documented as original."
  (if (catch 'found
	(let ((list fn-list)
	      val)
	  (while list
	    (if (setq val (efs-ftp-path (car list)))
		(throw 'found val)
	      (if (setq val (funcall name-constructor (car list)))
		  (throw 'found (efs-ftp-path val))
		(setq list (cdr list)))))))
      (progn
	(cond ((eq file-creator 'dired-copy-file)
	       (setq file-creator 'efs-dired-copy-file))
	      ((eq file-creator 'dired-rename-file)
	       (setq file-creator 'efs-dired-rename-file))
	      ((eq file-creator 'make-symbolic-link)
	       (setq file-creator 'efs-make-symbolic-link-internal))
	      ((eq file-creator 'add-name-to-file)
	       (setq file-creator 'efs-add-name-to-file-internal))
	      ((eq file-creator 'dired-make-relative-symlink)
	       (setq file-creator 'efs-dired-make-relative-symlink))
	      ((eq file-creator 'dired-compress-file)
	       (setq file-creator 'efs-dired-compress-file))
	      ((error "Unable to perform operation %s on remote hosts."
		      file-creator)))
	;; use the process-filter driven routine rather than the iterative one.
	(efs-dcf-1 file-creator operation fn-list name-constructor
		   (if (eq marker-char t)
		       (mapcar 'dired-file-marker fn-list)
		     marker-char)
		   query (buffer-name (current-buffer))
		   nil	;overwrite-query
		   nil	;dired-overwrite-backup-query
		   nil  ;dired-file-creator-query
		   nil	;failures
		   nil	;skipped
		   0		;success-count
		   (length fn-list) ;total
		   implicit-to
		   (and (eq file-creator 'efs-dired-rename-file)
			(delq nil
			      (mapcar
			       (function
				(lambda (x)
				  (and (assoc (file-name-as-directory x)
					      dired-subdir-alist)
				       x)))
			       fn-list)))))
    ;; normal case... use the interative routine... much cheaper.
    (efs-real-dired-create-files file-creator operation fn-list
				 name-constructor marker-char query
				 implicit-to)))

(defun efs-dcf-1 (file-creator operation fn-list name-constructor
			       markers query buffer-name overwrite-query 
			       overwrite-backup-query file-creator-query
			       failures skipped success-count total
			       implicit-to insertions)
  (if (null fn-list)
      (efs-dcf-3 failures operation total skipped
		 success-count buffer-name)
    (let* ((from (car fn-list))
	   ;; For dired-handle-overwrite and the file-creator-query,
	   ;; need to set these 2 fluid vars according to the cont data.
	   (dired-overwrite-backup-query overwrite-backup-query)
	   (dired-file-creator-query file-creator-query)
	   (to (funcall name-constructor from))
	   (marker-char (if (consp markers)
			    (prog1 (car markers)
			      (setq markers (cdr markers)))
			  markers))
	   (fn-list (cdr fn-list)))
      (if to
	  (if (equal to from)
	      (progn
		(dired-log buffer-name "Cannot %s to same file: %s\n"
			   (downcase operation) from)
		(efs-dcf-1 file-creator operation fn-list name-constructor
			   markers query buffer-name overwrite-query
			   dired-overwrite-backup-query
			   dired-file-creator-query failures
			   (cons (dired-make-relative from nil t) skipped)
			   success-count total implicit-to insertions))
	    (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)))
		       (overwrite-confirmed ; for dired-handle-overwrite
			(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)))))
		  (condition-case err
		      (let ((dired-unhandle-add-files
			     (cons to dired-unhandle-add-files)))
			(if implicit-to
			    (funcall file-creator from overwrite-confirmed
				     (list (function efs-dcf-2)
					   file-creator operation fn-list
					   name-constructor markers
					   query marker-char
					   buffer-name to from overwrite
					   overwrite-confirmed overwrite-query 
					   dired-overwrite-backup-query
					   dired-file-creator-query
					   failures skipped success-count
					   total implicit-to insertions)
				     t)
			  (apply file-creator from to overwrite-confirmed
				 (list (function efs-dcf-2)
				       file-creator operation fn-list
				       name-constructor markers
				       query marker-char
				       buffer-name to from overwrite
				       overwrite-confirmed overwrite-query 
				       dired-overwrite-backup-query
				       dired-file-creator-query
				       failures skipped success-count total
				       implicit-to insertions)
				 (if insertions
				     (list t insertions)
				   '(t)))))
		    (error      		; FILE-CREATOR aborted
		     (efs-dcf-2 'failed ;result
				(format "%s" err) ;line
				"" file-creator operation fn-list
				name-constructor markers query marker-char
				buffer-name to from overwrite
				overwrite-confirmed overwrite-query
				dired-overwrite-backup-query
				dired-file-creator-query failures skipped
				success-count total implicit-to insertions))))
	      (efs-dcf-1 file-creator operation fn-list name-constructor
			 markers query buffer-name overwrite-query
			 dired-overwrite-backup-query dired-file-creator-query
			 failures
			 (cons (dired-make-relative from nil t) skipped)
			 success-count total implicit-to insertions)))
	(efs-dcf-1 file-creator operation fn-list name-constructor
		   markers query buffer-name overwrite-query
		   dired-overwrite-backup-query dired-file-creator-query
		   failures (cons (dired-make-relative from nil t) skipped)
		   success-count total implicit-to insertions)))))

(defun efs-dcf-2 (result line cont-lines file-creator operation fn-list
			 name-constructor markers query marker-char
			 buffer-name to from overwrite overwrite-confirmed
			 overwrite-query overwrite-backup-query
			 file-creator-query failures skipped success-count
			 total implicit-to insertions)
  (if result
      (progn
	(setq failures (cons (dired-make-relative from nil t) failures))
	(dired-log buffer-name "%s `%s' to `%s' failed:\n%s\n"
		   operation from to line))
    (setq success-count (1+ success-count))
    (message "%s: %d of %d" operation success-count total)
    (let ((efs-ls-uncache t))
      (dired-add-file to marker-char)))
  ;; iterate again
  (efs-dcf-1 file-creator operation fn-list name-constructor
	     markers query buffer-name overwrite-query overwrite-backup-query
	     file-creator-query failures skipped success-count total
	     implicit-to insertions))

(defun efs-dcf-3 (failures operation total skipped success-count buffer-name)
  (cond
   (failures
    (dired-log-summary buffer-name (format "%s failed for %d of %d file%s"
					   operation (length failures) total
					   (dired-plural-s total)) failures))
   (skipped
    (dired-log-summary buffer-name (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)))))

;;; Running remote shell commands

;;; This support isn't very good. efs is really about a virtual file system,
;;; and not remote processes. What is really required is low-level
;;; support for start-process & call-process on remote hosts. This shouldn't
;;; be part of efs, although.

(defun efs-dired-shell-unhandle-file-name (filename)
  ;; Puts remote file names into a form where they can be passed to remsh.
  (nth 2 (efs-ftp-path filename)))

(defun efs-dired-shell-call-process (command dir &optional in-background)
  ;; Runs shell process on remote hosts.
  (let* ((parsed (efs-ftp-path dir))
	 (host (car parsed))
	 (user (nth 1 parsed))
	 (rdir (nth 2 parsed))
	 (file-name-handler-alist nil))
    (or (string-equal (efs-internal-directory-file-name dir)
		      (efs-expand-tilde "~" (efs-host-type host) host user))
	(string-match "^cd " command)
	(setq command (concat "cd " rdir "; " command)))
    (setq command
	  (format  "%s %s%s \"%s\""	; remsh -l USER does not work well
					; on a hp-ux machine I tried
		   efs-remote-shell-file-name host
		   (if efs-remote-shell-takes-user
		       (concat " -l " user)
		     "")
		   command))
    (message "Doing shell command on %s..." host)
    (dired-shell-call-process
     command (file-name-directory efs-tmp-name-template) in-background)))

;;; Dired commands for running local processes on remote files.
;;
;;  Lots of things in this section need to be re-thunk.

(defun efs-dired-call-process (program discard &rest arguments)
  "Documented as original."
  ;; PROGRAM is always one of those below in the cond in dired.el.
  ;; The ARGUMENTS are (nearly) always files.
  (if (efs-ftp-path default-directory)
      ;; Can't use efs-dired-host-type here because the current
      ;; buffer is *dired-check-process output*
      (condition-case oops
	  (cond
	   ((string-equal "efs-call-compress" program)
	    (apply 'efs-call-compress arguments))
	   ((string-equal "chmod" program)
	    (efs-call-chmod arguments))
	   (t (error "Unknown remote command: %s" program)))
	(ftp-error (dired-log (buffer-name (current-buffer))
			      (format "%s: %s, %s\n"
				      (nth 1 oops)
				      (nth 2 oops)
				      (nth 3 oops))))
	(error (dired-log (buffer-name (current-buffer))
			  (format "%s\n" (nth 1 oops)))))
    (apply 'call-process program nil (not discard) nil arguments)))

(defun efs-dired-make-compressed-filename (name &optional method)
  ;; Version of dired-make-compressed-filename for efs.
  ;; If NAME is in the syntax of a compressed file (according to
  ;; dired-compression-method-alist), return the data (a list) from this
  ;; alist on how to uncompress it. Otherwise, return a string, the
  ;; uncompressed form of this file name. This is computed using the optional
  ;; argument METHOD (a symbol). If METHOD is nil, the ambient value of
  ;; dired-compression-method is used.
  (let* ((host-type (efs-host-type (car (efs-ftp-path name))))
	 (ef-alist (if (memq host-type efs-single-extension-host-types)
		       (mapcar
			(function
			 (lambda (elt)
			   (list (car elt)
				 (mapconcat
				  (function
				   (lambda (char)
				     (if (= char ?.)
					 "-"
				       (char-to-string char))))
				  (nth 1 elt) "")
				 (nth 2 elt)
				 (nth 3 elt))))
			dired-compression-method-alist)
		     dired-compression-method-alist))
	 (alist ef-alist)
	 (len (length name))
	 ext ext-len result)
    (if (memq host-type efs-version-host-types)
	(setq name (efs-internal-file-name-sans-versions host-type name)))
    (if (memq host-type efs-case-insensitive-host-types)
	(let ((name (downcase name)))
	  (while alist
	    (if (and (> len
			(setq ext-len (length (setq ext (nth 1 (car alist))))))
		     (string-equal (downcase ext)
				   (substring name (- ext-len))))
		(setq result (car alist)
		      alist nil)
	      (setq alist (cdr alist)))))
      (while alist
	(if (and (> len
		    (setq ext-len (length (setq ext (nth 1 (car alist))))))
		 (string-equal ext (substring name (- ext-len))))
	    (setq result (car alist)
		  alist nil)
	  (setq alist (cdr alist)))))
    (or result
	(concat name
		(nth 1 (or (assq (or method dired-compression-method)
				 ef-alist)
			   (error "Unknown compression method: %s"
				  (or method dired-compression-method))))))))

(defun efs-dired-compress-file (file ok-flag &optional cont nowait)
  ;; Version of dired-compress-file for remote files.
  (let* ((compressed-fn (efs-dired-make-compressed-filename file))
	 (host (car (efs-ftp-path file)))
	 (host-type (efs-host-type host)))
    (cond ((file-symlink-p file)
	   (if cont
	       (efs-call-cont
		cont 'failed
		(format "Cannot compress %s, a symbolic link." file) "")
	     (signal 'file-error (list "Compress error:" file
				       "a symbolic link"))))
	  ((listp compressed-fn)
	   (let ((newname (substring (if (memq host-type
					       efs-version-host-types)
					 (efs-internal-file-name-sans-versions
					  host-type file)
				       file)
				      0 (- (length (nth 1 compressed-fn)))))
		 (program (nth 3 compressed-fn)))
	     (if (and (memq host-type efs-unix-host-types)
		      (null (efs-get-host-property host 'exec-failed))
		      (null (eq (efs-get-host-property
				 host
				 (intern
				  (concat
				   "exec-"
				   (efs-compress-progname (car program)))))
				'failed)))
		 (efs-call-remote-compress
		  program file newname t ok-flag
		  (efs-cont (result line cont-lines) (program file newname
							      cont nowait)
		    (if result
			(if (eq result 'unsupported)
			    (efs-call-compress program file newname
					       t t cont nowait)
			  (if cont
			      (efs-call-cont cont result line cont-lines)
			    (signal 'ftp-error
				    (list "Uncompressing file"
					  (format "FTP Error: \"%s\" " line)
					  file))))
		      (if cont (efs-call-cont cont result line cont-lines))))
		  nowait)
	       (efs-call-compress
		program file newname t ok-flag cont nowait)
	       newname)))
	  ((stringp compressed-fn)
	   (let ((program (nth 2 (assq dired-compression-method
				       dired-compression-method-alist))))
	     (if (and (memq host-type efs-unix-host-types)
		      (null (efs-get-host-property host 'exec-failed))
		      (null (eq (efs-get-host-property
				 host
				 (intern
				  (concat
				   "exec-"
				   (efs-compress-progname (car program)))))
				'failed)))
		 (efs-call-remote-compress
		  program file compressed-fn nil ok-flag
		  (efs-cont (result line cont-lines) (program file
							      compressed-fn
							      cont nowait)
		    (if result
			(if (eq result 'unsupported)
			    (efs-call-compress program file compressed-fn nil
					       t cont nowait)
			  (if cont
			      (efs-call-cont cont result line cont-lines)
			    (signal 'ftp-error
				    (list "Compressing file"
					  (format "FTP Error: \"%s\" " line)
					  file))))
		      (if cont (efs-call-cont cont result line cont-lines))))
		  nowait)
	       (efs-call-compress
		program file compressed-fn nil ok-flag cont nowait)))
	   compressed-fn)
	  (t (error "Strange error in efs-dired-compress-file.")))))

(defun efs-dired-print-file (command file)
  ;; Version of dired-print-file for remote files.
  (let ((command (dired-trans-command command (list file) "")))
    ;; Only replace the first occurence of the file name?
    (if (string-match (concat "[ ><|]\\(" (regexp-quote
					   (dired-shell-quote file))
			      "\\)\\($\\|[ |><&]\\)")
		      command)
	(setq command (concat (substring command 0 (match-beginning 1))
			      "%s"
			      (substring command (match-end 1))))
      (error "efs-print-command: strange error"))
  (efs-call-lpr file command)))

;;;;----------------------------------------------------------------
;;;; Support for `processes' run on remote files.
;;;; Usually (but not necessarily) these are only called from dired.
;;;;----------------------------------------------------------------

(defun efs-compress-progname (program)
  ;; Returns a canonicalized i.e. without the "un", version of a compress
  ;; program name.
  (efs-save-match-data
    (if (string-equal program "gunzip")
	"gzip"
      (if (string-match "^un" program)
	  (substring program (match-end 0))
	program))))

(defun efs-call-remote-compress (program filename newname &optional uncompress
					 ok-if-already-exists cont nowait)
  ;; Run a remote compress process using SITE EXEC.
  (if (or (not ok-if-already-exists)
	  (numberp ok-if-already-exists))
      (efs-barf-or-query-if-file-exists
       newname
       (if uncompress
	   "uncompress to it"
	 "compress to it")
       (numberp ok-if-already-exists)))
  (let* ((filename (expand-file-name filename))
	 (parsed (efs-ftp-path filename))
	 (host (car parsed))
	 (user (nth 1 parsed))
	 (rpath (nth 2 parsed)))
    (if (efs-get-host-property host 'exec-failed)
	(if cont
	    (efs-call-cont cont 'unsupported "SITE EXEC not supported" "")
	  (signal 'ftp-error (list "Unable to SITE EXEC" host)))
      (let* ((progname (efs-compress-progname (car program)))
	     (propsym (intern  (concat "exec-" progname)))
	     (prop (efs-get-host-property host propsym)))
	(cond
	 ((eq prop 'failed)
	  (if cont
	      (efs-call-cont cont 'unsupported
			     (concat progname " not in FTP exec path") "")
	    (signal 'ftp-error
		    (list (concat progname " not in FTP exec path") host))))
	 ((eq prop 'worked)
	  (efs-send-cmd
	   host user
	   (list 'quote 'site 'exec
		 (concat (mapconcat 'identity program " ") " " rpath))
	   (concat (if uncompress "Uncompressing " "Compressing ") filename)
	   nil
	   (efs-cont (result line cont-lines) (host user filename cont)
	     (if result
		 (progn
		   (efs-set-host-property host 'exec-failed t)
		   (efs-error host user (concat "FTP exec Error: " line)))
	       (efs-save-match-data
		 (if (string-match "\n200-\\([^\n]*\\)" cont-lines)
		     (let ((err (substring cont-lines (match-beginning 1)
					   (match-end 1))))
		       (if cont
			   (efs-call-cont cont  'failed err cont-lines)
			 (efs-error host user (concat "FTP Error: " err))))
		   ;; This function only gets called for unix hosts, so
		   ;; we'll use the default version of efs-delete-file-entry
		   ;; and save a host-type lookup.
		   (efs-delete-file-entry nil filename)
		   (dired-remove-file filename)
		   (if cont (efs-call-cont cont nil line cont-lines))))))
	   nowait))
	 (t ; (null prop)
	  (efs-send-cmd
	   host user
	   (list 'quote 'site 'exec (concat progname " " "-V"))
	   (format "Checking for %s executable" progname)
	   nil
	   (efs-cont (result line cont-lines) (propsym host program filename
						       newname uncompress
						       cont nowait)
	     (efs-save-match-data
	       (if (string-match "\n200-" cont-lines)
		   (efs-set-host-property host propsym 'worked)
		 (efs-set-host-property host propsym 'failed)))
	     (efs-call-remote-compress program filename newname uncompress
				       t ; already tested for overwrite
				       cont nowait))
	   nowait)))))))

(defun efs-call-compress (program filename newname &optional uncompress
				  ok-if-already-exists cont nowait)
  "Perform a compress command on a remote file.
PROGRAM is a list of the compression program and args. Works by taking a 
copy of the file, compressing it and copying the file back. Returns 0 on
success, 1 or 2 on failure. If UNCOMPRESS is non-nil, does this instead."
  (let* ((filename (expand-file-name filename))
	 (newname (expand-file-name newname))
	 (parsed (efs-ftp-path filename))
	 (tmp1 (car (efs-make-tmp-name nil (car parsed))))
	 (tmp2 (car (efs-make-tmp-name nil (car parsed))))
	 (program (mapconcat 'identity program " ")))
    (efs-copy-file-internal
     filename parsed tmp1 nil
     t nil 2
     (efs-cont (result line cont-lines) (filename newname tmp1 tmp2 program
				       uncompress ok-if-already-exists
				       cont nowait)
       (if result
	   (signal 'ftp-error
		   (list "Opening input file"
			 (format "FTP Error: \"%s\" " line) filename))
	 (let ((err-buff (let ((default-major-mode 'fundamental-mode))
			   (get-buffer-create
			    (generate-new-buffer-name
			     (format
			      " efs-call-compress %s" filename))))))
	   (save-excursion
	     (set-buffer err-buff)
	     (set (make-local-variable 'efs-call-compress-filename) filename)
	     (set (make-local-variable 'efs-call-compress-newname) newname)
	     (set (make-local-variable 'efs-call-compress-tmp1) tmp1)
	     (set (make-local-variable 'efs-call-compress-tmp2) tmp2)
	     (set (make-local-variable 'efs-call-compress-cont) cont)
	     (set (make-local-variable 'efs-call-compress-nowait) nowait)
	     (set (make-local-variable 'efs-call-compress-ok)
		  ok-if-already-exists)
	     (set (make-local-variable 'efs-call-compress-uncompress)
		  uncompress)
	     (set (make-local-variable 'efs-call-compress-abbr)
		  (efs-relativize-filename filename))
	     (if efs-verbose
		 (efs-message
		  (format "%s %s..."
			  (if uncompress "Uncompressing" "Compressing")
			  (symbol-value (make-local-variable
					 'efs-call-compress-abbr)))))
	     (set-process-sentinel
	      (start-process (format "efs-call-compress %s" filename)
			     err-buff shell-file-name
			     "-c" (format "%s %s < %s > %s"
					  program
					  ;; Hope -c makes the compress
					  ;; program write to std out.
					  "-c"
					  tmp1 tmp2))
	      (function
	       (lambda (proc str)
		 (let ((buff (get-buffer (process-buffer proc))))
		   (if buff
		       (save-excursion
			 (set-buffer buff)
			 (if (/= (buffer-size) 0)
			     (if cont
				 (efs-call-cont
				  (symbol-value
				   (make-local-variable
				    'efs-call-compress-cont))
				  'failed
				  (concat
				   "failed to compress "
				   (symbol-value (make-local-variable
						  'efs-call-compress-filename))
				   ", "
				   (buffer-substring
				    (point-min)
				    (progn (goto-char (point-min))
					   (end-of-line) (point))))))
			   (efs-del-tmp-name (symbol-value
					      (make-local-variable
					       'efs-call-compress-tmp1)))
			   (let ((tmp2 (symbol-value
					(make-local-variable
					 'efs-call-compress-tmp2)))
				 (newname (symbol-value
					   (make-local-variable
					    'efs-call-compress-newname)))
				 (filename (symbol-value
					    (make-local-variable
					     'efs-call-compress-filename)))
				 (cont (symbol-value
					(make-local-variable
					 'efs-call-compress-cont)))
				 (nowait (symbol-value
					  (make-local-variable
					   'efs-call-compress-nowait)))
				 (ok (symbol-value
				      (make-local-variable
				       'efs-call-compress-ok)))
				 (uncompress
				  (symbol-value
				   (make-local-variable
				    'efs-call-compress-uncompress))))
			     (if efs-verbose
				 (efs-message
				  (format "%s %s...done"
					  (if uncompress
					      "Uncompressing"
					    "Compressing")
					  (symbol-value
					   (make-local-variable
					    'efs-call-compress-abbr)))))
			     (kill-buffer (current-buffer))
			     (efs-copy-file-internal
			      tmp2 nil newname (efs-ftp-path newname)
			      ok nil 1
			      (efs-cont (result line cont-lines) (cont
								  tmp2
								  filename)
				(efs-del-tmp-name tmp2)
				(or result
				    (let (efs-verbose)
				      (efs-delete-file filename)
				      (dired-remove-file filename)))
				(if cont
				    (efs-call-cont cont result line
						   cont-lines)))
			      nowait (if uncompress nil 'image)))))
		     (error "Strange error: %s" proc))))))))))
     nowait (if uncompress 'image nil))))

(defun efs-update-mode-string (perms modes)
  ;; For PERMS of the form `u+w', and MODES a unix 9-character mode string,
  ;; computes the new mode string.
  ;; Doesn't call efs-save-match-data. The calling function should.
  (or (string-match "^[augo]+\\([+-]\\)[rwxst]+$" perms)
      (error "efs-update-mode-string: invalid perms %s" perms))
  (let* ((who (substring perms 0 (match-beginning 1)))
	 (add (= (aref perms (match-beginning 1)) ?+))
	 (what (substring perms (match-end 1)))
	 (newmodes (copy-sequence modes))
	 (read (string-match "r" what))
	 (write (string-match "w" what))
	 (execute (string-match "x" what))
	 (sticky (string-match "t" what))
	 (suid (string-match "s" what)))
    (if (string-match "a" who)
	(if add
	    (progn
	      (if read
		  (progn
		    (aset newmodes 0 ?r)
		    (aset newmodes 3 ?r)
		    (aset newmodes 6 ?r)))
	      (if write
		  (progn
		    (aset newmodes 1 ?w)
		    (aset newmodes 4 ?w)
		    (aset newmodes 7 ?w)))
	      (if execute
		  (let ((curr (aref newmodes 2)))
		    (if (= curr ?-)
			(aset newmodes 2 ?x)
		      (if (= curr ?S)
			  (aset newmodes 2 ?s)))
		    (setq curr (aref newmodes 5))
		    (if (= curr ?-)
			(aset newmodes 5 ?x)
		      (if (= curr ?S)
			  (aset newmodes 5 ?s)))
		    (setq curr (aref newmodes 8))
		    (if (= curr ?-)
			(aset newmodes 8 ?x)
		      (if (= curr ?T)
			  (aset newmodes 8 ?t)))))
	      (if suid
		  (let ((curr (aref newmodes 2)))
		    (if (= curr ?-)
			(aset newmodes 2 ?S)
		      (if (= curr ?x)
			  (aset newmodes 2 ?s)))
		    (setq curr (aref newmodes 5))
		    (if (= curr ?-)
			(aset newmodes 5 ?S)
		      (if (= curr ?x)
			  (aset newmodes 5 ?s)))))
	      (if sticky
		  (let ((curr (aref newmodes 8)))
		    (if (= curr ?-)
			(aset newmodes 8 ?T)
		      (if (= curr ?x)
			  (aset newmodes 8 ?t))))))
	  (if read
	      (progn
		(aset newmodes 0 ?-)
		(aset newmodes 3 ?-)
		(aset newmodes 6 ?-)))
	  (if write
	      (progn
		(aset newmodes 1 ?-)
		(aset newmodes 4 ?-)
		(aset newmodes 7 ?-)))
	  (if execute
	      (let ((curr (aref newmodes 2)))
		(if (= curr ?x)
		    (aset newmodes 2 ?-)
		  (if (= curr ?s)
		      (aset newmodes 2 ?S)))
		(setq curr (aref newmodes 5))
		(if (= curr ?x)
		    (aset newmodes 5 ?-)
		  (if (= curr ?s)
		      (aset newmodes 5 ?S)))
		    (setq curr (aref newmodes 8))
		    (if (= curr ?x)
			(aset newmodes 8 ?-)
		      (if (= curr ?t)
			  (aset newmodes 8 ?T)))))
	  (if suid
	      (let ((curr (aref newmodes 2)))
		(if (= curr ?s)
		    (aset newmodes 2 ?x)
		  (if (= curr ?S)
		      (aset newmodes 2 ?-)))
		(setq curr (aref newmodes 5))
		(if (= curr ?s)
		    (aset newmodes 5 ?x)
		  (if (= curr ?S)
		      (aset newmodes 5 ?-)))))
	  (if sticky
	      (let ((curr (aref newmodes 8)))
		(if (= curr ?t)
		    (aset newmodes 8 ?x)
		  (if (= curr ?T)
		      (aset newmodes 8 ?-))))))
      (if (string-match "u" who)
	  (if add
	      (progn
		(if read
		    (aset newmodes 0 ?r))
		(if write
		    (aset newmodes 1 ?w))
		(if execute
		    (let ((curr (aref newmodes 2)))
		      (if (= curr ?-)
			  (aset newmodes 2 ?x)
			(if (= curr ?S)
			    (aset newmodes 2 ?s)))))
		(if suid
		    (let ((curr (aref newmodes 2)))
		      (if (= curr ?-)
			  (aset newmodes 2 ?S)
			(if (= curr ?x)
			    (aset newmodes 2 ?s))))))
	    (if read
		(aset newmodes 0 ?-))
	    (if write
		(aset newmodes 1 ?-))
	    (if execute
		(let ((curr (aref newmodes 2)))
		  (if (= curr ?x)
		      (aset newmodes 2 ?-)
		    (if (= curr ?s)
			(aset newmodes 2 ?S)))))
	    (if suid
		(let ((curr (aref newmodes 2)))
		  (if (= curr ?s)
		      (aset newmodes 2 ?x)
		    (if (= curr ?S)
			(aset newmodes 2 ?-)))))))
      (if (string-match "g" who)
	  (if add
	      (progn
		(if read
		    (aset newmodes 3 ?r))
		(if write
		    (aset newmodes 4 ?w))
		(if execute
		    (let ((curr (aref newmodes 5)))
		      (if (= curr ?-)
			  (aset newmodes 5 ?x)
			(if (= curr ?S)
			    (aset newmodes 5 ?s)))))
		(if suid
		    (let ((curr (aref newmodes 5)))
		      (if (= curr ?-)
			  (aset newmodes 5 ?S)
			(if (= curr ?x)
			    (aset newmodes 5 ?s))))))
	    (if read
		(aset newmodes 3 ?-))
	    (if write
		(aset newmodes 4 ?-))
	    (if execute
		(let ((curr (aref newmodes 5)))
		  (if (= curr ?x)
		      (aset newmodes 5 ?-)
		    (if (= curr ?s)
			(aset newmodes 5 ?S)))))
	    (if suid
		(let ((curr (aref newmodes 5)))
		  (if (= curr ?s)
		      (aset newmodes 5 ?x)
		    (if (= curr ?S)
			(aset newmodes 5 ?-)))))))
      (if (string-match "o" who)
	  (if add
	      (progn
		(if read
		    (aset newmodes 6 ?r))
		(if write
		    (aset newmodes 7 ?w))
		(if execute
		    (let ((curr (aref newmodes 8)))
		      (if (= curr ?-)
			  (aset newmodes 8 ?x)
			(if (= curr ?T)
			    (aset newmodes 8 ?t)))))
		(if sticky
		    (let ((curr (aref newmodes 8)))
		      (if (= curr ?-)
			  (aset newmodes 8 ?T)
			(if (= curr ?x)
			    (aset newmodes 5 ?t))))))
	    (if read
		(aset newmodes 6 ?-))
	    (if write
		(aset newmodes 7 ?-))
	    (if execute
		(let ((curr (aref newmodes 8)))
		  (if (= curr ?x)
		      (aset newmodes 8 ?-)
		    (if (= curr ?t)
			(aset newmodes 8 ?T)))))
	    (if suid
		(let ((curr (aref newmodes 8)))
		  (if (= curr ?t)
		      (aset newmodes 8 ?x)
		    (if (= curr ?T)
			(aset newmodes 8 ?-))))))))
    newmodes))

(defun efs-compute-chmod-arg (perms file)
  ;; Computes the octal number, represented as a string, required to 
  ;; modify the permissions PERMS of FILE.
  (efs-save-match-data
    (cond
     ((string-match "^[0-7][0-7]?[0-7]?[0-7]?$" perms)
      perms)
     ((string-match "^[augo]+[-+][rwxst]+$" perms)
      (let ((curr-mode (nth 3 (efs-get-file-entry file))))
	(or (and curr-mode
		 (stringp curr-mode)
		 (= (length curr-mode) 10))
	    (progn
	      ;; Current buffer is process error buffer
	      (insert "Require an octal integer to modify modes for "
		      file ".\n")
	      (error "Require an octal integer to modify modes for %s." file)))
	(format "%o"
		(efs-parse-mode-string
		 (efs-update-mode-string perms
					      (substring curr-mode 1))))))
     (t
      (insert "Don't know how to set modes " perms " for " file ".\n")
      (error "Don't know how to set modes %s" perms)))))

(defun efs-call-chmod (args)
  ;; Sends an FTP CHMOD command.
  (if (< (length args) 2)
      (error "efs-call-chmod: missing mode and/or filename: %s" args))
  (let ((mode (car args))
	bombed)
    (mapcar
     (function
      (lambda (file)
	(setq file (expand-file-name file))
	(let ((parsed (efs-ftp-path file)))
	  (if parsed
	      (condition-case nil
		  (let* ((mode (efs-compute-chmod-arg mode file))
			 (host (nth 0 parsed))
			 (user (nth 1 parsed))
			 (path (efs-quote-string
				(efs-host-type host user) (nth 2 parsed)))
			 (abbr (efs-relativize-filename file))
			 (result (efs-send-cmd host user
					       (list 'quote 'site 'chmod
						     mode path)
					       (format "doing chmod %s"
						       abbr))))

		    (if (car result)
			(efs-dired-shell-call-process
 			 (concat "chmod " mode " " (file-name-nondirectory file))
 			 (file-name-directory file)))

		    (efs-del-from-ls-cache file t))
		(error (setq bombed t)))))))
     (cdr args))
    (if bombed 1 0)))                      ; return code

(defun efs-call-lpr (file command-format)
  "Print remote file FILE. SWITCHES are passed to the print program."
  ;; Works asynch.
  (let* ((file (expand-file-name file))
	 (parsed (efs-ftp-path file))
	 (abbr (efs-relativize-filename file))
	 (temp (car (efs-make-tmp-name nil (car parsed)))))
    (efs-copy-file-internal
     file parsed temp nil t nil 2
     (efs-cont (result line cont-lines) (command-format file abbr temp)
       (if result
	   (signal 'ftp-error (list "Opening input file"
				    (format "FTP Error: \"%s\" " line)
				    file))
	 (message "Spooling %s..." abbr)
	 (set-process-sentinel
	  (start-process (format "*print %s /// %s*" abbr temp)
			 (generate-new-buffer-name " *print temp*")
			 "sh" "-c" (format command-format temp))
	  (function
	   (lambda (proc status)
	     (let ((buff (process-buffer proc))
		   (name (process-name proc)))
	       (if (and buff (get-buffer buff))
		   (unwind-protect
		       (save-excursion
			 (set-buffer buff)
			 (if (> (buffer-size) 0)
			     (let ((log-buff (get-buffer-create
					      "*Shell Command Output*")))
			       (set-buffer log-buff)
			       (goto-char (point-max))
			       (or (bobp)
				   (insert "\n"))
			       (insert-buffer-substring buff)
			       (goto-char (point-max))
			       (display-buffer log-buff))))
		     (condition-case nil (kill-buffer buff) (error nil))
		     (efs-save-match-data
		       (if (string-match "^\\*print \\(.*\\) /// \\(.*\\)\\*$"
					 name)
			   (let ((abbr (substring name (match-beginning 1)
						  (match-end 1)))
				 (temp (substring name (match-beginning 2)
						  (match-end 2))))
			     (or (= (match-beginning 2) (match-end 2))
				 (efs-del-tmp-name temp))
			     (message "Spooling %s...done" abbr))))))))))))
     t)))

;;;; --------------------------------------------------------------
;;;; Attaching onto dired.
;;;; --------------------------------------------------------------

;;; Look out for MULE
(if (or (boundp 'MULE) (featurep 'mule)) (load "efs-dired-mule"))

;;; Magic file name hooks for dired.

(put 'dired-print-file 'efs 'efs-dired-print-file)
(put 'dired-make-compressed-filename 'efs 'efs-dired-make-compressed-filename)
(put 'dired-compress-file 'efs 'efs-dired-compress-file)
(put 'dired-recursive-delete-directory 'efs
     'efs-dired-recursive-delete-directory)
(put 'dired-uncache 'efs 'efs-dired-uncache)
(put 'dired-shell-call-process 'efs 'efs-dired-shell-call-process)
(put 'dired-shell-unhandle-file-name 'efs 'efs-dired-shell-unhandle-file-name)
(put 'dired-file-modtime 'efs 'efs-dired-file-modtime)
(put 'dired-set-file-modtime 'efs 'efs-dired-set-file-modtime)

;;; Overwriting functions

(efs-overwrite-fn "efs" 'dired-call-process)
(efs-overwrite-fn "efs" 'dired-insert-headerline)
(efs-overwrite-fn "efs" 'dired-manual-move-to-filename)
(efs-overwrite-fn "efs" 'dired-manual-move-to-end-of-filename)
(efs-overwrite-fn "efs" 'dired-make-filename-string)
(efs-overwrite-fn "efs" 'dired-flag-backup-files)
(efs-overwrite-fn "efs" 'dired-create-files)
(efs-overwrite-fn "efs" 'dired-find-file)
(efs-overwrite-fn "efs" 'dired-find-file-other-window)
(efs-overwrite-fn "efs" 'dired-find-file-other-frame)
(efs-overwrite-fn "efs" 'dired-collect-file-versions)
(efs-overwrite-fn "efs" 'dired-file-name-lessp)

;;; Hooks

(add-hook 'dired-before-readin-hook 'efs-dired-before-readin)

;;; Handle dired-grep.el too.

(if (featurep 'dired-grep)
    (efs-overwrite-fn "efs" 'dired-grep-delete-local-temp-file
		      'efs-diff/grep-del-temp-file)
  (add-hook 'dired-grep-load-hook
	    (function
	     (lambda ()
	       (efs-overwrite-fn "efs" 'dired-grep-delete-local-temp-file
				 'efs-diff/grep-del-temp-file)))))

;;; end of efs-dired.el