home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / elisp / packages / ange-ftp.el < prev    next >
Encoding:
Text File  |  1992-08-14  |  219.7 KB  |  5,954 lines

  1. ;; -*-Emacs-Lisp-*-
  2. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  3. ;;
  4. ;; File:         ange-ftp.el
  5. ;; RCS:          $Header: ange-ftp.el,v 4.20 92/08/14 17:04:34 ange Exp $
  6. ;; Description:  transparent FTP support for GNU Emacs
  7. ;; Author:       Andy Norman, ange@hplb.hpl.hp.com
  8. ;; Created:      Thu Oct 12 14:00:05 1989
  9. ;; Modified:     Fri Aug 14 17:03:57 1992 (Ange) ange@anorman
  10. ;;
  11. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  12.  
  13. ;;; Copyright (C) 1989, 1990, 1991, 1992  Andy Norman.
  14. ;;;
  15. ;;; Author: Andy Norman (ange@hplb.hpl.hp.com)
  16. ;;;
  17. ;;; This program is free software; you can redistribute it and/or modify
  18. ;;; it under the terms of the GNU General Public License as published by
  19. ;;; the Free Software Foundation; either version 1, or (at your option)
  20. ;;; any later version.
  21. ;;;
  22. ;;; This program is distributed in the hope that it will be useful,
  23. ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  25. ;;; GNU General Public License for more details.
  26. ;;;
  27. ;;; A copy of the GNU General Public License can be obtained from this
  28. ;;; program's author (send electronic mail to ange@hplb.hpl.hp.com) or from
  29. ;;; the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA
  30. ;;; 02139, USA.
  31.  
  32. ;;; Description:
  33. ;;;
  34. ;;; This package attempts to make accessing files and directories using FTP
  35. ;;; from within GNU Emacs as simple and transparent as possible.  A subset of
  36. ;;; the common file-handling routines are extended to interact with FTP.
  37.  
  38. ;;; Installation:
  39. ;;;
  40. ;;; Byte-compile ange-ftp.el to ange-ftp.elc and put them both in a directory
  41. ;;; on your load-path.  Load the package from your .emacs file with:
  42. ;;;
  43. ;;;   (require 'ange-ftp).
  44. ;;;
  45. ;;; ange-ftp can't sensibly be auto-loaded; you are either using it, or you
  46. ;;; ain't.
  47.  
  48. ;;; Usage:
  49. ;;;
  50. ;;; Some of the common GNU Emacs file-handling operations have been made
  51. ;;; FTP-smart.  If one of these routines is given a filename that matches
  52. ;;; '/user@host:path' then it will spawn an FTP process connecting to machine
  53. ;;; 'host' as account 'user' and perform its operation on the file 'path'.
  54. ;;;
  55. ;;; For example: if find-file is given a filename of:
  56. ;;;
  57. ;;;   /ange@anorman:/tmp/notes
  58. ;;;
  59. ;;; then ange-ftp will spawn an FTP process, connect to the host 'anorman' as
  60. ;;; user 'ange', get the file '/tmp/notes' and pop up a buffer containing the
  61. ;;; contents of that file as if it were on the local filesystem.  If ange-ftp
  62. ;;; needed a password to connect then it would prompt the user in the
  63. ;;; minibuffer.
  64.  
  65. ;;; Extended filename syntax:
  66. ;;;
  67. ;;; The default extended filename syntax is '/user@host:path', where the
  68. ;;; 'user@' part may be omitted.  This syntax can be customised to a certain
  69. ;;; extent by changing ange-ftp-path-format.  There are limitations.
  70. ;;;
  71. ;;; If the user part is omitted then ange-ftp will generate a default user
  72. ;;; instead whose value depends on the variable ange-ftp-default-user.
  73.  
  74. ;;; Passwords:
  75. ;;;
  76. ;;; A password is required for each host / user pair.  This will be prompted
  77. ;;; for when needed, unless already set by calling ange-ftp-set-passwd, or
  78. ;;; specified in a *valid* ~/.netrc file.
  79.  
  80. ;;; Passwords for user "anonymous":
  81. ;;;
  82. ;;; Passwords for the user "anonymous" (or "ftp") are handled specially.  The
  83. ;;; variable ange-ftp-generate-anonymous-password controls what happens: if
  84. ;;; the value of this variable is a string, then this is used as the password;
  85. ;;; if non-nil, then a password is created from the name of the user and the
  86. ;;; hostname of the machine on which GNU Emacs is running; if nil (the
  87. ;;; default) then the user is prompted for a password as normal.
  88.  
  89. ;;; "Dumb" UNIX hosts:
  90. ;;;
  91. ;;; The FTP servers on some UNIX machines have problems if the 'ls' command is
  92. ;;; used.
  93. ;;;
  94. ;;; The routine ange-ftp-add-dumb-unix-host can be called to tell ange-ftp to
  95. ;;; limit itself to the DIR command and not 'ls' for a given UNIX host.  Note
  96. ;;; that this change will take effect for the current GNU Emacs session only.
  97. ;;; See below for a discussion of non-UNIX hosts.  If a large number of
  98. ;;; machines with similar hostnames have this problem then it is easier to set
  99. ;;; the value of ange-ftp-dumb-unix-host-regexp in your .emacs file. ange-ftp
  100. ;;; is unable to automatically recognize dumb unix hosts.
  101.  
  102. ;;; File name completion:
  103. ;;;
  104. ;;; Full file-name completion is supported on UNIX, VMS, CMS, and MTS hosts.
  105. ;;; To do filename completion, ange-ftp needs a listing from the remote host.
  106. ;;; Therefore, for very slow connections, it might not save any time.
  107.  
  108. ;;; FTP processes:
  109. ;;;
  110. ;;; When ange-ftp starts up an FTP process, it leaves it running for speed
  111. ;;; purposes.  Some FTP servers will close the connection after a period of
  112. ;;; time, but ange-ftp should be able to quietly reconnect the next time that
  113. ;;; the process is needed.
  114. ;;;
  115. ;;; The FTP process will be killed should the associated "*ftp user@host*"
  116. ;;; buffer be deleted.  This should not cause ange-ftp any grief.
  117.  
  118. ;;; Binary file transfers:
  119. ;;;
  120. ;;; By default ange-ftp will transfer files in ASCII mode.  If a file being
  121. ;;; transferred matches the value of ange-ftp-binary-file-name-regexp then the
  122. ;;; FTP process will be toggled into BINARY mode before the transfer and back
  123. ;;; to ASCII mode after the transfer.
  124.  
  125. ;;; Account passwords:
  126. ;;;
  127. ;;; Some FTP servers require an additional password which is sent by the
  128. ;;; ACCOUNT command.  ange-ftp partially supports this by allowing the user to
  129. ;;; specify an account password by either calling ange-ftp-set-account, or by
  130. ;;; specifying an account token in the .netrc file.  If the account password
  131. ;;; is set by either of these methods then ange-ftp will issue an ACCOUNT
  132. ;;; command upon starting the FTP process.
  133.  
  134. ;;; Preloading:
  135. ;;;
  136. ;;; ange-ftp can be preloaded, but must be put in the site-init.el file and
  137. ;;; not the site-load.el file in order for the documentation strings for the
  138. ;;; functions being overloaded to be available.
  139.  
  140. ;;; Status reports:
  141. ;;;
  142. ;;; Most ange-ftp commands that talk to the FTP process output a status
  143. ;;; message on what they are doing.  In addition, ange-ftp can take advantage
  144. ;;; of the FTP client's HASH command to display the status of transferring
  145. ;;; files and listing directories.  See the documentation for the variables
  146. ;;; ange-ftp-{ascii,binary}-hash-mark-size, ange-ftp-send-hash and
  147. ;;; ange-ftp-process-verbose for more details.
  148.  
  149. ;;; Gateways:
  150. ;;;
  151. ;;; Sometimes it is neccessary for the FTP process to be run on a different
  152. ;;; machine than the machine running GNU Emacs.  This can happen when the
  153. ;;; local machine has restrictions on what hosts it can access.
  154. ;;;
  155. ;;; ange-ftp has support for running the ftp process on a different (gateway)
  156. ;;; machine.  The way it works is as follows:
  157. ;;;
  158. ;;; 1) Set the variable 'ange-ftp-gateway-host' to the name of a machine
  159. ;;;    that doesn't have the access restrictions.
  160. ;;;
  161. ;;; 2) Set the variable 'ange-ftp-local-host-regexp' to a regular expression
  162. ;;;    that matches hosts that can be contacted from running a local ftp
  163. ;;;    process, but fails to match hosts that can't be accessed locally.  For
  164. ;;;    example:
  165. ;;;
  166. ;;;    "\\.hp\\.com$\\|^[^.]*$"
  167. ;;;
  168. ;;;    will match all hosts that are in the .hp.com domain, or don't have an
  169. ;;;    explicit domain in their name, but will fail to match hosts with
  170. ;;;    explicit domains or that are specified by their ip address.
  171. ;;;
  172. ;;; 3) Using NFS and symlinks, make sure that there is a shared directory with
  173. ;;;    the *same* name between the local machine and the gateway machine.
  174. ;;;    This directory is neccessary for temporary files created by ange-ftp.
  175. ;;;
  176. ;;; 4) Set the variable 'ange-ftp-gateway-tmp-name-template' to the name of
  177. ;;;    this directory plus an identifying filename prefix.  For example:
  178. ;;;
  179. ;;;    "/nfs/hplose/ange/ange-ftp"
  180. ;;;
  181. ;;;    where /nfs/hplose/ange is a directory that is shared between the
  182. ;;;    gateway machine and the local machine.
  183. ;;;
  184. ;;; The simplest way of getting a ftp process running on the gateway machine
  185. ;;; is if you can spawn a remote shell using either 'rsh' or 'remsh'.  If you
  186. ;;; can't do this for some reason such as security then points 7 onwards will
  187. ;;; discuss an alternative approach.
  188. ;;;
  189. ;;; 5) Set the variable ange-ftp-gateway-program to the name of the remote
  190. ;;;    shell process such as 'remsh' or 'rsh' if the default isn't correct.
  191. ;;;
  192. ;;; 6) Set the variable ange-ftp-gateway-program-interactive to nil if it
  193. ;;;    isn't already.  This tells ange-ftp that you are using a remote shell
  194. ;;;    rather than logging in using telnet or rlogin.
  195. ;;;
  196. ;;; That should be all you need to allow ange-ftp to spawn a ftp process on
  197. ;;; the gateway machine.  If you have to use telnet or rlogin to get to the
  198. ;;; gateway machine then follow the instructions below.
  199. ;;;
  200. ;;; 7) Set the variable ange-ftp-gateway-program to the name of the program
  201. ;;;    that lets you log onto the gateway machine.  This may be something like
  202. ;;;    telnet or rlogin.
  203. ;;;
  204. ;;; 8) Set the variable ange-ftp-gateway-prompt-pattern to a regular
  205. ;;;    expression that matches the prompt you get when you login to the
  206. ;;;    gateway machine.  Be very specific here; this regexp must not match
  207. ;;;    *anything* in your login banner except this prompt.
  208. ;;;    shell-prompt-pattern is far too general as it appears to match some
  209. ;;;    login banners from Sun machines.  For example:
  210. ;;;
  211. ;;;    "^$*$ *"
  212. ;;;
  213. ;;; 9) Set the variable ange-ftp-gateway-program-interactive to 't' to let
  214. ;;;    ange-ftp know that it has to "hand-hold" the login to the gateway
  215. ;;;    machine.
  216. ;;;
  217. ;;; 10) Set the variable ange-ftp-gateway-setup-term-command to a UNIX command
  218. ;;;     that will put the pty connected to the gateway machine into a
  219. ;;;     no-echoing mode, and will strip off carriage-returns from output from
  220. ;;;     the gateway machine.  For example:
  221. ;;;
  222. ;;;     "stty -onlcr -echo"
  223. ;;;
  224. ;;;     will work on HP-UX machines, whereas:
  225. ;;;
  226. ;;;     "stty -echo nl"
  227. ;;;
  228. ;;;     appears to work for some Sun machines.
  229. ;;;
  230. ;;; That's all there is to it.
  231.  
  232. ;;; Smart gateways:
  233. ;;;
  234. ;;; If you have a "smart" ftp program that allows you to issue commands like
  235. ;;; "USER foo@bar" which do nice proxy things, then look at the variables
  236. ;;; ange-ftp-smart-gateway and ange-ftp-smart-gateway-port.
  237.  
  238. ;;; Tips for using ange-ftp:
  239. ;;;
  240. ;;; 1. For dired to work on a host which marks symlinks with a trailing @ in
  241. ;;;    an ls -alF listing, you need to (setq dired-ls-F-marks-symlinks t).
  242. ;;;    Most UNIX systems do not do this, but ULTRIX does. If you think that
  243. ;;;    there is a chance you might connect to an ULTRIX machine (such as
  244. ;;;    prep.ai.mit.edu), then set this variable accordingly.  This will have
  245. ;;;    the side effect that dired will have problems with symlinks whose names
  246. ;;;    end in an @. If you get youself into this situation then editing
  247. ;;;    dired's ls-switches to remove "F", will temporarily fix things.
  248. ;;;
  249. ;;; 2. If you know that you are connecting to a certain non-UNIX machine
  250. ;;;    frequently, and ange-ftp seems to be unable to guess its host-type,
  251. ;;;    then setting the appropriate host-type regexp
  252. ;;;    (ange-ftp-vms-host-regexp, ange-ftp-mts-host-regexp, or
  253. ;;;    ange-ftp-cms-host-regexp) accordingly should help. Also, please report
  254. ;;;    ange-ftp's inability to recognize the host-type as a bug.
  255. ;;;
  256. ;;; 3. For slow connections, you might get "listing unreadable" error
  257. ;;;    messages, or get an empty buffer for a file that you know has something
  258. ;;;    in it. The solution is to increase the value of ange-ftp-retry-time.
  259. ;;;    Its default value is 5 which is plenty for reasonable connections.
  260. ;;;    However, for some transatlantic connections I set this to 20.
  261. ;;;
  262. ;;; 4. Beware of compressing files on non-UNIX hosts. Ange-ftp will do it by
  263. ;;;    copying the file to the local machine, compressing it there, and then
  264. ;;;    sending it back. Binary file transfers between machines of different
  265. ;;;    architectures can be a risky business. Test things out first on some
  266. ;;;    test files. See "Bugs" below. Also, note that ange-ftp copies files by
  267. ;;;    moving them through the local machine. Again, be careful when doing
  268. ;;;    this with binary files on non-Unix machines.
  269. ;;;
  270. ;;; 5. Beware that dired over ftp will use your setting of dired-no-confirm
  271. ;;;    (list of dired commands for which confirmation is not asked).  You
  272. ;;;    might want to reconsider your setting of this variable, because you
  273. ;;;    might want confirmation for more commands on remote direds than on
  274. ;;;    local direds. For example, I strongly recommend that you not include
  275. ;;;    compress and uncompress in this list. If there is enough demand it
  276. ;;;    might be a good idea to have an alist ange-ftp-dired-no-confirm of
  277. ;;;    pairs ( TYPE . LIST ), where TYPE is an operating system type and LIST
  278. ;;;    is a list of commands for which confirmation would be suppressed.  Then
  279. ;;;    remote dired listings would take their (buffer-local) value of
  280. ;;;    dired-no-confirm from this alist. Who votes for this?
  281.  
  282. ;;; ---------------------------------------------------------------------
  283. ;;; Non-UNIX support:
  284. ;;; ---------------------------------------------------------------------
  285.  
  286. ;;; VMS support:
  287. ;;;
  288. ;;; Ange-ftp has full support for VMS hosts, including tree dired support.  It
  289. ;;; should be able to automatically recognize any VMS machine. However, if it
  290. ;;; fails to do this, you can use the command ange-ftp-add-vms-host.  As well,
  291. ;;; you can set the variable ange-ftp-vms-host-regexp in your .emacs file. We
  292. ;;; would be grateful if you would report any failures to automatically
  293. ;;; recognize a VMS host as a bug.
  294. ;;;
  295. ;;; Filename Syntax:
  296. ;;;
  297. ;;; For ease of *implementation*, the user enters the VMS filename syntax in a
  298. ;;; UNIX-y way.  For example:
  299. ;;;  PUB$:[ANONYMOUS.SDSCPUB.NEXT]README.TXT;1
  300. ;;; would be entered as:
  301. ;;;  /PUB$$:/ANONYMOUS/SDSCPUB/NEXT/README.TXT;1
  302. ;;; i.e. to log in as anonymous on ymir.claremont.edu and grab the file:
  303. ;;;  [.CSV.POLICY]RULES.MEM
  304. ;;; you would type:
  305. ;;;  C-x C-f /anonymous@ymir.claremont.edu:CSV/POLICY/RULES.MEM
  306. ;;;
  307. ;;; A legal VMS filename is of the form: FILE.TYPE;##
  308. ;;; where FILE can be up to 39 characters
  309. ;;;       TYPE can be up to 39 characters
  310. ;;;       ## is a version number (an integer between 1 and 32,767)
  311. ;;; Valid characters in FILE and TYPE are A-Z 0-9 _ - $
  312. ;;; $ cannot begin a filename, and - cannot be used as the first or last
  313. ;;; character.
  314. ;;;
  315. ;;; Tips:
  316. ;;; 1. Although VMS is not case sensitive, EMACS running under UNIX is.
  317. ;;;    Therefore, to access a VMS file, you must enter the filename with upper
  318. ;;;    case letters.
  319. ;;; 2. To access the latest version of file under VMS, you use the filename
  320. ;;;    without the ";" and version number. You should always edit the latest
  321. ;;;    version of a file. If you want to edit an earlier version, copy it to a
  322. ;;;    new file first. This has nothing to do with ange-ftp, but is simply
  323. ;;;    good VMS operating practice. Therefore, to edit FILE.TXT;3 (say 3 is
  324. ;;;    latest version), do C-x C-f /ymir.claremont.edu:FILE.TXT. If you
  325. ;;;    inadvertently do C-x C-f /ymir.claremont.edu:FILE.TXT;3, you will find
  326. ;;;    that VMS will not allow you to save the file because it will refuse to
  327. ;;;    overwrite FILE.TXT;3, but instead will want to create FILE.TXT;4, and
  328. ;;;    attach the buffer to this file. To get out of this situation, M-x
  329. ;;;    write-file /ymir.claremont.edu:FILE.TXT will attach the buffer to
  330. ;;;    latest version of the file. For this reason, in tree dired "f"
  331. ;;;    (dired-find-file), always loads the file sans version, whereas "v",
  332. ;;;    (dired-view-file), always loads the explicit version number. The
  333. ;;;    reasoning being that it reasonable to view old versions of a file, but
  334. ;;;    not to edit them.
  335. ;;; 3. EMACS has a feature in which it does environment variable substitution
  336. ;;;    in filenames. Therefore, to enter a $ in a filename, you must quote it
  337. ;;;    by typing $$. There is a bug in EMACS, in that it neglects to quote the
  338. ;;;    $'s in the default directory when it writes it in the minibuffer.  You
  339. ;;;    must edit the minibuffer to quote the $'s manually. Hopefully, this bug
  340. ;;;    will be fixed in EMACS 19. If you use Sebastian Kremer's gmhist (V 4.26
  341. ;;;    or newer), you will not have this problem.
  342.  
  343. ;;; MTS support:
  344. ;;;
  345. ;;; Ange-ftp has full support, including tree dired support, for hosts running
  346. ;;; the Michigan terminal system.  It should be able to automatically
  347. ;;; recognize any MTS machine. However, if it fails to do this, you can use
  348. ;;; the command ange-ftp-add-mts-host.  As well, you can set the variable
  349. ;;; ange-ftp-mts-host-regexp in your .emacs file. We would be grateful if you
  350. ;;; would report any failures to automatically recognize a MTS host as a bug.
  351. ;;;
  352. ;;; Filename syntax:
  353. ;;; 
  354. ;;; MTS filenames are entered in a UNIX-y way. For example, if your account
  355. ;;; was YYYY, the file FILE in the account XXXX: on mtsg.ubc.ca would be
  356. ;;; entered as
  357. ;;;   /YYYY@mtsg.ubc.ca:/XXXX:/FILE
  358. ;;; In other words, MTS accounts are treated as UNIX directories. Of course,
  359. ;;; to access a file in another account, you must have access permission for
  360. ;;; it.  If FILE were in your own account, then you could enter it in a
  361. ;;; relative path fashion as
  362. ;;;   /YYYY@mtsg.ubc.ca:FILE
  363. ;;; MTS filenames can be up to 12 characters. Like UNIX, the structure of the
  364. ;;; filename does not contain a TYPE (i.e. it can have as many "."'s as you
  365. ;;; like.) MTS filenames are always in upper case, and hence be sure to enter
  366. ;;; them as such! MTS is not case sensitive, but an EMACS running under UNIX
  367. ;;; is.
  368.  
  369. ;;; CMS support:
  370. ;;; 
  371. ;;; Ange-ftp has full support, including tree dired support, for hosts running
  372. ;;; CMS.  It should be able to automatically recognize any CMS machine.
  373. ;;; However, if it fails to do this, you can use the command
  374. ;;; ange-ftp-add-cms-host.  As well, you can set the variable
  375. ;;; ange-ftp-cms-host-regexp in your .emacs file. We would be grateful if you
  376. ;;; would report any failures to automatically recognize a CMS host as a bug.
  377. ;;; 
  378. ;;; Filename syntax:
  379. ;;;
  380. ;;; CMS filenames are entered in a UNIX-y way. In otherwords, minidisks are
  381. ;;; treated as UNIX directories. For example to access the file READ.ME in
  382. ;;; minidisk *.311 on cuvmb.cc.columbia.edu, you would enter
  383. ;;;   /anonymous@cuvmb.cc.columbia.edu:/*.311/READ.ME
  384. ;;; If *.301 is the default minidisk for this account, you could access
  385. ;;; FOO.BAR on this minidisk as
  386. ;;;   /anonymous@cuvmb.cc.columbia.edu:FOO.BAR
  387. ;;; CMS filenames are of the form FILE.TYPE, where both FILE and TYPE can be
  388. ;;; up to 8 characters. Again, beware that CMS filenames are always upper
  389. ;;; case, and hence must be entered as such.
  390. ;;;
  391. ;;; Tips:
  392. ;;; 1. CMS machines, with the exception of anonymous accounts, nearly always
  393. ;;;    need an account password. To have ange-ftp send an account password,
  394. ;;;    you can either include it in your .netrc file, or use
  395. ;;;    ange-ftp-set-account.
  396. ;;; 2. Ange-ftp cannot send "write passwords" for a minidisk. Hopefully, we
  397. ;;;    can fix this.
  398. ;;;
  399. ;;; ------------------------------------------------------------------
  400. ;;; Bugs:
  401. ;;; ------------------------------------------------------------------
  402. ;;; 
  403. ;;; 1. Umask problems:
  404. ;;;    Be warned that files created by using ange-ftp will take account of the
  405. ;;;    umask of the ftp daemon process rather than the umask of the creating
  406. ;;;    user.  This is particulary important when logging in as the root user.
  407. ;;;    The way that I tighten up the ftp daemon's umask under HP-UX is to make
  408. ;;;    sure that the umask is changed to 027 before I spawn /etc/inetd.  I
  409. ;;;    suspect that there is something similar on other systems.
  410. ;;;
  411. ;;; 2. Some combinations of FTP clients and servers break and get out of sync
  412. ;;;    when asked to list a non-existent directory.  Some of the ai.mit.edu
  413. ;;;    machines cause this problem for some FTP clients. Using
  414. ;;;    ange-ftp-kill-process can be used to restart the ftp process, which
  415. ;;;    should get things back in synch.
  416. ;;;
  417. ;;; 3. Ange-ftp does not check to make sure that when creating a new file,
  418. ;;;    you provide a valid filename for the remote operating system.
  419. ;;;    If you do not, then the remote FTP server will most likely
  420. ;;;    translate your filename in some way. This may cause ange-ftp to
  421. ;;;    get confused about what exactly is the name of the file. The
  422. ;;;    most common causes of this are using lower case filenames on systems
  423. ;;;    which support only upper case, and using filenames which are too
  424. ;;;    long.
  425. ;;;
  426. ;;; 4. Null (blank) passwords confuse both ange-ftp and some FTP daemons.
  427. ;;;
  428. ;;; 5. Ange-ftp likes to use pty's to talk to its FTP processes.  If GNU Emacs
  429. ;;;    for some reason creates a FTP process that only talks via pipes then
  430. ;;;    ange-ftp won't be getting the information it requires at the time that
  431. ;;;    it wants it since pipes flush at different times to pty's.  One
  432. ;;;    disgusting way around this problem is to talk to the FTP process via
  433. ;;;    rlogin which does the 'right' things with pty's.
  434. ;;;
  435. ;;; 6. For CMS support, we send too many cd's. Since cd's are cheap, I haven't
  436. ;;;    worried about this too much. Eventually, we should have some caching
  437. ;;;    of the current minidisk.
  438. ;;;    
  439. ;;; 7. Some CMS machines do not assign a default minidisk when you ftp them as
  440. ;;;    anonymous. It is then necessary to guess a valid minidisk name, and cd
  441. ;;;    to it. This is (understandably) beyond ange-ftp.
  442. ;;;
  443. ;;; 8. Remote to remote copying of files on non-Unix machines can be risky.
  444. ;;;    Depending on the variable ange-ftp-binary-file-name-regexp, ange-ftp
  445. ;;;    will use binary mode for the copy. Between systems of different
  446. ;;;    architecture, this still may not be enough to guarantee the integrity
  447. ;;;    of binary files. Binary file transfers from VMS machines are
  448. ;;;    particularly problematical. Should ange-ftp-binary-file-name-regexp be
  449. ;;;    an alist of OS type, regexp pairs?
  450. ;;;
  451. ;;; 9. The code to do compression of files over ftp is not as careful as it
  452. ;;;    should be. It deletes the old remote version of the file, before
  453. ;;;    actually checking if the local to remote transfer of the compressed
  454. ;;;    file succeeds. Of course to delete the original version of the file
  455. ;;;    after transferring the compressed version back is also dangerous,
  456. ;;;    because some OS's have severe restrictions on the length of filenames,
  457. ;;;    and when the compressed version is copied back the "-Z" or ".Z" may be
  458. ;;;    truncated. Then, ange-ftp would delete the only remaining version of
  459. ;;;    the file.  Maybe ange-ftp should make backups when it compresses files
  460. ;;;    (of course, the backup "~" could also be truncated off, sigh...).
  461. ;;;    Suggestions?
  462. ;;;
  463.  
  464. ;;; 10. If a dir listing is attempted for an empty directory on (at least
  465. ;;;     some) VMS hosts, an ftp error is given. This is really an ftp bug, and
  466. ;;;     I don't know how to get ange-ftp work to around it.
  467. ;;;
  468. ;;; 11. Bombs on filenames that start with a space. Deals well with filenames
  469. ;;;     containing spaces, but beware that the remote ftpd may not like them
  470. ;;;     much.
  471. ;;;
  472. ;;; 12. No classic dired support for non-UNIX systems. Tree dired was enough.
  473. ;;; 
  474. ;;; 13. The famous @ bug. As mentioned above in TIPS, ULTRIX marks symlinks
  475. ;;;     with a trailing @ in a ls -alF listing. In order to account for this
  476. ;;;     ange-ftp looks to chop trailing @'s off of symlink names when it is
  477. ;;;     parsing a listing with the F switch. This will cause ange-ftp to
  478. ;;;     incorrectly get the name of a symlink on a non-ULTRIX host if its name
  479. ;;;     ends in an @. ange-ftp will correct itself if you take F out of the
  480. ;;;     dired ls switches (C-u s will allow you to edit the switches). The
  481. ;;;     dired buffer will be automatically reverted, which will allow ange-ftp
  482. ;;;     to fix its files hashtable.  A cookie to anyone who can think of a
  483. ;;;     fast, sure-fire way to recognize ULTRIX over ftp.
  484.  
  485. ;;; If you find any bugs or problems with this package, PLEASE either e-mail
  486. ;;; the above author, or send a message to the ange-ftp-lovers mailing list
  487. ;;; below.  Ideas and constructive comments are especially welcome.
  488.  
  489. ;;; ange-ftp-lovers:
  490. ;;;
  491. ;;; ange-ftp has its own mailing list modestly called ange-ftp-lovers.  All
  492. ;;; users of ange-ftp are welcome to subscribe (see below) and to discuss
  493. ;;; aspects of ange-ftp.  New versions of ange-ftp are posted periodically to
  494. ;;; the mailing list.
  495. ;;;
  496. ;;; To [un]subscribe to ange-ftp-lovers, or to report mailer problems with the
  497. ;;; list, please mail one of the following addresses:
  498. ;;;
  499. ;;;     ange-ftp-lovers-request@anorman.hpl.hp.com
  500. ;;; or
  501. ;;;     ange-ftp-lovers-request%anorman.hpl.hp.com@hplb.hpl.hp.com
  502. ;;;
  503. ;;; Please don't forget the -request part.
  504. ;;;
  505. ;;; For mail to be posted directly to ange-ftp-lovers, send to one of the
  506. ;;; following addresses:
  507. ;;; 
  508. ;;;     ange-ftp-lovers@anorman.hpl.hp.com
  509. ;;; or
  510. ;;;     ange-ftp-lovers%anorman.hpl.hp.com@hplb.hpl.hp.com
  511. ;;;
  512. ;;; Alternatively, there is a mailing list that only gets announcements of new
  513. ;;; ange-ftp releases.  This is called ange-ftp-lovers-announce, and can be
  514. ;;; subscribed to by e-mailing to the -request address as above.  Please make
  515. ;;; it clear in the request which mailing list you wish to join.
  516.  
  517. ;;; The latest version of ange-ftp can usually be obtained via anonymous ftp
  518. ;;; from:
  519. ;;;     alpha.gnu.ai.mit.edu:ange-ftp/ange-ftp.tar.Z
  520. ;;; or:
  521. ;;;     ugle.unit.no:/pub/gnu/emacs-lisp/ange-ftp.tar.Z
  522. ;;; or:
  523. ;;;   archive.cis.ohio-state.edu:pub/gnu/emacs/elisp-archive/packages/ange-ftp.tar.Z
  524.  
  525. ;;; The archives for ange-ftp-lovers can be found via anonymous ftp under:
  526. ;;;
  527. ;;;     ftp.reed.edu:pub/mailing-lists/ange-ftp/
  528.  
  529. ;;; -----------------------------------------------------------
  530. ;;; Technical information on this package:
  531. ;;; -----------------------------------------------------------
  532.  
  533. ;;; The following GNU Emacs functions are replaced by this package:
  534. ;;;
  535. ;;;   write-region
  536. ;;;   insert-file-contents
  537. ;;;   dired-readin
  538. ;;;   dired-revert
  539. ;;;   dired-call-process
  540. ;;;   diff
  541. ;;;   delete-file
  542. ;;;   read-file-name-internal
  543. ;;;   verify-visited-file-modtime
  544. ;;;   directory-files
  545. ;;;   backup-buffer
  546. ;;;   file-directory-p
  547. ;;;   file-writable-p
  548. ;;;   file-exists-p
  549. ;;;   file-readable-p
  550. ;;;   file-symlink-p
  551. ;;;   file-attributes
  552. ;;;   copy-file
  553. ;;;   rename-file
  554. ;;;   file-name-as-directory
  555. ;;;   file-name-directory
  556. ;;;   file-name-nondirectory
  557. ;;;   file-name-completion
  558. ;;;   directory-file-name
  559. ;;;   expand-file-name
  560. ;;;   file-name-all-completions
  561.  
  562. ;;; LISPDIR ENTRY for the Elisp Archive
  563. ;;; 
  564. ;;;    LCD Archive Entry:
  565. ;;;    ange-ftp|Andy Norman|ange@hplb.hpl.hp.com
  566. ;;;    |transparent FTP Support for GNU Emacs
  567. ;;;    |$Date: 92/08/14 17:04:34 $|$Revision: 4.20 $|
  568.  
  569. ;;; Checklist for adding non-UNIX support for TYPE
  570. ;;; 
  571. ;;; The following functions may need TYPE versions:
  572. ;;; (not all functions will be needed for every OS)
  573. ;;;
  574. ;;; ange-ftp-fix-path-for-TYPE
  575. ;;; ange-ftp-fix-dir-path-for-TYPE
  576. ;;; ange-ftp-TYPE-host
  577. ;;; ange-ftp-TYPE-add-host
  578. ;;; ange-ftp-parse-TYPE-listing
  579. ;;; ange-ftp-TYPE-delete-file-entry
  580. ;;; ange-ftp-TYPE-add-file-entry
  581. ;;; ange-ftp-TYPE-file-name-as-directory
  582. ;;;
  583. ;;; Variables:
  584. ;;;
  585. ;;; ange-ftp-TYPE-host-regexp
  586. ;;; May need to add TYPE to ange-ftp-dumb-host-types
  587. ;;;
  588. ;;; Check the following functions for OS dependent coding:
  589. ;;;
  590. ;;; ange-ftp-host-type
  591. ;;; ange-ftp-guess-host-type
  592. ;;; ange-ftp-allow-child-lookup
  593. ;;;
  594. ;;; For Tree Dired support:
  595. ;;;
  596. ;;; ange-ftp-dired-TYPE-insert-headerline
  597. ;;; ange-ftp-dired-TYPE-move-to-filename
  598. ;;; ange-ftp-dired-TYPE-move-to-end-of-filename
  599. ;;; ange-ftp-dired-TYPE-get-filename
  600. ;;; ange-ftp-dired-TYPE-between-files
  601. ;;; ange-ftp-TYPE-make-compressed-filename
  602. ;;; ange-ftp-dired-TYPE-ls-trim
  603. ;;; ange-ftp-TYPE-bob-version
  604. ;;; ange-ftp-dired-TYPE-clean-directory
  605. ;;; ange-ftp-dired-TYPE-flag-backup-files
  606. ;;; ange-ftp-dired-TYPE-backup-diff
  607. ;;;
  608. ;;; Variables for dired:
  609. ;;;
  610. ;;; ange-ftp-dired-TYPE-re-exe
  611. ;;; ange-ftp-dired-TYPE-re-dir
  612.  
  613. ;;; Host type conventions:
  614. ;;;
  615. ;;; The function ange-ftp-host-type and the variable ange-ftp-dired-host-type
  616. ;;; (mostly) follow the following conventions for remote host types.  At
  617. ;;; least, I think that future code should try to follow these conventions,
  618. ;;; and the current code should eventually be made compliant.
  619. ;;;
  620. ;;; nil = local host type, whatever that is (probably unix).
  621. ;;;       Think nil as in "not a remote host". This value is used by
  622. ;;;       ange-ftp-dired-host-type for local buffers.
  623. ;;;
  624. ;;; t = a remote host of unknown type. Think t is in true, it's remote.
  625. ;;;     Currently, 'unix is used as the default remote host type.
  626. ;;;     Maybe we should use t.
  627. ;;;
  628. ;;; 'type = a remote host of TYPE type.
  629. ;;;
  630. ;;; 'type:list = a remote host of TYPE type, using a specialized ftp listing
  631. ;;;              program called list. This is currently only used for Unix
  632. ;;;              dl (descriptive listings), when ange-ftp-dired-host-type
  633. ;;;              is set to 'unix:dl.
  634.  
  635. ;;; Bug report codes:
  636. ;;;
  637. ;;; Because of their naive faith in this code, there are certain situations
  638. ;;; which the writers of this program believe could never happen. However,
  639. ;;; being realists they have put calls to 'error in the program at these
  640. ;;; points. These errors provide a code, which is an integer, greater than 1.
  641. ;;; To aid debugging.  the error codes, and the functions in which they reside
  642. ;;; are listed below.
  643. ;;; 
  644. ;;; 1: See ange-ftp-ls
  645. ;;;
  646.  
  647. ;;; -----------------------------------------------------------
  648. ;;; Hall of fame:
  649. ;;; -----------------------------------------------------------
  650. ;;; 
  651. ;;; Thanks to Roland McGrath for improving the filename syntax handling,
  652. ;;; for suggesting many enhancements and for numerous cleanups to the code.
  653. ;;;
  654. ;;; Thanks to Jamie Zawinski for bugfixes and for ideas such as gateways.
  655. ;;;
  656. ;;; Thanks to Ken Laprade for improved .netrc parsing, password reading, and
  657. ;;; dired / shell auto-loading.
  658. ;;;
  659. ;;; Thanks to Sebastian Kremer for tree dired support and for many ideas and
  660. ;;; bugfixes.
  661. ;;;
  662. ;;; Thanks to Joe Wells for bugfixes, the original non-UNIX system support,
  663. ;;; VOS support, and hostname completion.
  664. ;;;
  665. ;;; Thanks to Nakagawa Takayuki for many good ideas, filename-completion, help
  666. ;;; with file-name expansion, efficiency worries, stylistic concerns and many
  667. ;;; bugfixes.
  668. ;;;
  669. ;;; Thanks to Sandy Rutherford who re-wrote most of ange-ftp to support VMS,
  670. ;;; MTS, CMS and UNIX-dls.  Sandy also added dired-support for non-UNIX OS and
  671. ;;; auto-recognition of the host type.
  672. ;;;
  673. ;;; Thanks to Dave Smith who wrote the info file for ange-ftp.
  674. ;;;
  675. ;;; Finally, thanks to Keith Waclena, Mark D. Baushke, Terence Kelleher, Ping
  676. ;;; Zhou, Edward Vielmetti, Jack Repenning, Mike Balenger, Todd Kaufmann,
  677. ;;; Kjetil Svarstad, Tom Wurgler, Linus Tolke, Niko Makila, Carl Edman, Bill
  678. ;;; Trost, Dave Brennan, Dan Jacobson, Andy Scott, Steve Anderson, Sanjay
  679. ;;; Mathur, the folks on the ange-ftp-lovers mailing list and many others
  680. ;;; whose names I've forgotten who have helped to debug and fix problems with
  681. ;;; ange-ftp.el.
  682.  
  683. ;;;; ------------------------------------------------------------
  684. ;;;; User customization variables.
  685. ;;;; ------------------------------------------------------------
  686.  
  687. (defvar ange-ftp-path-format
  688.   '("^/\\(\\([^@/:]*\\)@\\)?\\([^@/:]*\\):\\(.*\\)" . (3 2 4))
  689.   "*Format of a fully expanded remote pathname.  This is a cons
  690. \(REGEXP . \(HOST USER PATH\)\), where REGEXP is a regular expression matching
  691. the full remote pathname, and HOST, USER, and PATH are the numbers of
  692. parenthesized expressions in REGEXP for the components (in that order).")
  693.  
  694. ;; ange-ftp-multi-skip-msgs should only match ###-, where ### is one of
  695. ;; the number codes corresponding to ange-ftp-good-msgs or ange-ftp-fatal-msgs.
  696. ;; Otherwise, ange-ftp will go into multi-skip mode, and never come out.
  697.  
  698. (defvar ange-ftp-multi-msgs
  699.   "^220-\\|^230-\\|^226\\|^25.-\\|^221-\\|^200-\\|^530-\\|^4[25]1-"
  700.   "*Regular expression matching messages from the ftp process that start
  701. a multiline reply.")
  702.  
  703. (defvar ange-ftp-good-msgs
  704.   "^220 \\|^230 \\|^226 \\|^25. \\|^221 \\|^200 \\|^[Hh]ash mark"
  705.   "*Regular expression matching messages from the ftp process that indicate
  706. that the action that was initiated has completed successfully.")
  707.  
  708. ;; CMS and the odd VMS machine say 200 Port rather than 200 PORT.
  709. ;; Also CMS machines use a multiline 550- reply to say that you
  710. ;; don't have write permission. ange-ftp gets into multi-line skip
  711. ;; mode and hangs. Have it ignore 550- instead. It will then barf
  712. ;; when it gets the 550 line, as it should.
  713.  
  714. (defvar ange-ftp-skip-msgs
  715.   (concat "^200 \\(PORT\\|Port\\) \\|^331 \\|^150 \\|^350 \\|^[0-9]+ bytes \\|"
  716.       "^Connected \\|^$\\|^Remote system\\|^Using\\|^ \\|Password:\\|"
  717.       "^local:\\|^Trying\\|^125 \\|^550-\\|^221 .*oodbye")
  718.   "*Regular expression matching messages from the ftp process that can be
  719. ignored.")
  720.  
  721. (defvar ange-ftp-fatal-msgs
  722.   (concat "^ftp: \\|^Not connected\\|^530 \\|^4[25]1 \\|rcmd: \\|"
  723.       "^No control connection\\|unknown host\\|^lost connection")
  724.   "*Regular expression matching messages from the FTP process that indicate
  725. something has gone drastically wrong attempting the action that was
  726. initiated and that the FTP process should (or already has) been killed.")
  727.  
  728. (defvar ange-ftp-gateway-fatal-msgs
  729.   "No route to host\\|Connection closed\\|No such host\\|Login incorrect"
  730.   "*Regular expression matching messages from the rlogin / telnet process that
  731. indicates that logging in to the gateway machine has gone wrong.")
  732.  
  733. (defvar ange-ftp-xfer-size-msgs
  734.   "^150 .* connection for .* (\\([0-9]+\\) bytes)"
  735.   "*Regular expression used to determine the number of bytes in a FTP transfer.")
  736.  
  737. (defvar ange-ftp-tmp-name-template "/tmp/ange-ftp"
  738.   "*Template used to create temporary files.")
  739.  
  740. (defvar ange-ftp-gateway-tmp-name-template "/tmp/ange-ftp"
  741.   "*Template used to create temporary files when ftp-ing through a gateway.
  742. Files starting with this prefix need to be accessible from BOTH the local
  743. machine and the gateway machine, and need to have the SAME name on both
  744. machines, that is, /tmp is probably NOT what you want, since that is rarely
  745. cross-mounted.")
  746.  
  747. (defvar ange-ftp-netrc-filename "~/.netrc"
  748.   "*File in .netrc format to search for passwords.")
  749.  
  750. (defvar ange-ftp-disable-netrc-security-check nil
  751.   "*If non-nil avoid checking permissions on the .netrc file.")
  752.  
  753. (defvar ange-ftp-default-user nil
  754.   "*User name to use when none is specied in a pathname.
  755. If nil, then the name under which the user is logged in is used.
  756. If non-nil but not a string, the user is prompted for the name.")
  757.  
  758. (defvar ange-ftp-default-password nil
  759.   "*Password to use when the user is the same as ange-ftp-default-user.")
  760.  
  761. (defvar ange-ftp-default-account nil
  762.   "*Account password to use when the user is the same as ange-ftp-default-user.")
  763.  
  764. (defvar ange-ftp-generate-anonymous-password nil
  765.   "*If t, use a password of user@host when logging in as the anonymous user.
  766. If a string then use that as the password.
  767. If nil then prompt the user for a password.")
  768.  
  769. (defvar ange-ftp-dumb-unix-host-regexp nil
  770.   "*If non-nil, if the host being ftp'd to matches this regexp then the FTP
  771. process uses the \'dir\' command to get directory information.")
  772.  
  773. (defvar ange-ftp-binary-file-name-regexp
  774.   (concat "\\.Z$\\|\\.lzh$\\|\\.arc$\\|\\.zip$\\|\\.zoo$\\|\\.tar$\\|"
  775.       "\\.dvi$\\|\\.ps$\\|\\.elc$\\|TAGS$\\|\\.gif$\\|"
  776.       "\\.EXE\\(;[0-9]+\\)?$\\|\\.Z-part-..$")
  777.   "*If a file matches this regexp then it is transferred in binary mode.")
  778.  
  779. (defvar ange-ftp-gateway-host nil
  780.   "*Name of host to use as gateway machine when local FTP isn't possible.")
  781.  
  782. (defvar ange-ftp-local-host-regexp ".*"
  783.   "*If a host being FTP'd to matches this regexp then the ftp process is started
  784. locally, otherwise the FTP process is started on \`ange-ftp-gateway-host\'
  785. instead.")
  786.  
  787. (defvar ange-ftp-gateway-program-interactive nil
  788.   "*If non-nil then the gateway program is expected to connect to the gateway
  789. machine and eventually give a shell prompt.  Both telnet and rlogin do something
  790. like this.")
  791.  
  792. (defvar ange-ftp-gateway-program (if (eq system-type 'hpux) "remsh" "rsh")
  793.   "*Name of program to spawn a shell on the gateway machine.  Valid candidates
  794. are rsh (remsh on hp-ux), telnet and rlogin.  See also the gateway variable
  795. above.")
  796.  
  797. (defvar ange-ftp-gateway-prompt-pattern "^[^#$%>;]*[#$%>;] *"
  798.   "*Regexp used to detect that the logging-in sequence is completed on the
  799. gateway machine and that the shell is now awaiting input.  Make this regexp as
  800. strict as possible; it shouldn't match *anything* at all except the user's
  801. initial prompt.  The above string will fail under most SUN-3's since it
  802. matches the login banner.")
  803.  
  804. (defvar ange-ftp-gateway-setup-term-command
  805.   (if (eq system-type 'hpux)
  806.       "stty -onlcr -echo\n"
  807.     "stty -echo nl\n")
  808.   "*Command to use after logging in to the gateway machine to stop the terminal
  809. echoing each command and to strip out trailing ^M characters.")
  810.  
  811. (defvar ange-ftp-smart-gateway nil
  812.   "*If the gateway FTP is smart enough to use proxy server, then don't bother
  813. telnetting etc, just issue a user@host command instead.")
  814.  
  815. (defvar ange-ftp-smart-gateway-port "21"
  816.   "*Port on gateway machine to use when smart gateway is in operation.")
  817.  
  818. (defvar ange-ftp-send-hash t
  819.   "*If non-nil, send the HASH command to the FTP client.")
  820.  
  821. (defvar ange-ftp-binary-hash-mark-size nil
  822.   "*Default size, in bytes, between hash-marks when transferring a binary file.
  823. If NIL, this variable will be locally overridden if the FTP client outputs a
  824. suitable response to the HASH command.  If non-NIL then this value takes
  825. precedence over the local value.")
  826.  
  827. (defvar ange-ftp-ascii-hash-mark-size 1024
  828.   "*Default size, in bytes, between hash-marks when transferring an ASCII file.
  829. This variable is buffer-local and will be locally overridden if the FTP client
  830. outputs a suitable response to the HASH command.")
  831.  
  832. (defvar ange-ftp-process-verbose t
  833.   "*If non-NIL then be chatty about interaction with the FTP process.")
  834.  
  835. (defvar ange-ftp-ftp-program-name "ftp"
  836.   "*Name of FTP program to run.")
  837.  
  838. (defvar ange-ftp-gateway-ftp-program-name "ftp"
  839.   "*Name of FTP program to run on gateway machine.
  840. Some AT&T folks claim to use something called `pftp' here.")
  841.  
  842. (defvar ange-ftp-ftp-program-args '("-i" "-n" "-g" "-v")
  843.   "*A list of arguments passed to the FTP program when started.")
  844.  
  845. (defvar ange-ftp-nslookup-program nil
  846.   "*If non-NIL then a string naming nslookup program." )
  847.  
  848. (defvar ange-ftp-make-backup-files ()
  849.   "*A list of operating systems for which ange-ftp will make Emacs backup
  850. files files on the remote host. For example, '\(unix\) makes sense, but
  851. '\(unix vms\) or '\(vms\) would be silly, since vms makes its own backups.")
  852.  
  853. (defvar ange-ftp-retry-time 5
  854.   "*Number of seconds to wait before retrying if a file or listing
  855. doesn't arrive. This might need to be increased for very slow connections.")
  856.  
  857. (defvar ange-ftp-auto-save 0
  858.   "If 1, allows ange-ftp files to be auto-saved.
  859. If 0, suppresses auto-saving of ange-ftp files.
  860. Don't use any other value.")
  861.  
  862. ;;;; ------------------------------------------------------------
  863. ;;;; Hash table support.
  864. ;;;; ------------------------------------------------------------
  865.  
  866. (require 'backquote)
  867.  
  868. (defun ange-ftp-make-hashtable (&optional size)
  869.   "Make an obarray suitable for use as a hashtable.
  870. SIZE, if supplied, should be a prime number."
  871.   (make-vector (or size 31) 0))
  872.  
  873. (defun ange-ftp-map-hashtable (fun tbl)
  874.   "Call FUNCTION on each key and value in HASHTABLE."
  875.   (mapatoms
  876.    (function 
  877.     (lambda (sym)
  878.       (funcall fun (get sym 'key) (get sym 'val))))
  879.    tbl))
  880.  
  881. (defmacro ange-ftp-make-hash-key (key)
  882.   "Convert KEY into a suitable key for a hashtable."
  883.   (` (if (stringp (, key))
  884.      (, key)
  885.        (prin1-to-string (, key)))))
  886.  
  887. (defun ange-ftp-get-hash-entry (key tbl)
  888.   "Return the value associated with KEY in HASHTABLE."
  889.   (let ((sym (intern-soft (ange-ftp-make-hash-key key) tbl)))
  890.     (and sym (get sym 'val))))
  891.  
  892. (defun ange-ftp-put-hash-entry (key val tbl)
  893.   "Record an association between KEY and VALUE in HASHTABLE."
  894.   (let ((sym (intern (ange-ftp-make-hash-key key) tbl)))
  895.     (put sym 'val val)
  896.     (put sym 'key key)))
  897.  
  898. (defun ange-ftp-del-hash-entry (key tbl)
  899.   "Copy all symbols except KEY in HASHTABLE and return modified hashtable."
  900.   (let* ((len (length tbl))
  901.      (new-tbl (ange-ftp-make-hashtable len))
  902.      (i (1- len)))
  903.     (ange-ftp-map-hashtable
  904.      (function
  905.       (lambda (k v)
  906.     (or (equal k key)
  907.         (ange-ftp-put-hash-entry k v new-tbl))))
  908.      tbl)
  909.     (while (>= i 0)
  910.       (aset tbl i (aref new-tbl i))
  911.       (setq i (1- i)))
  912.     tbl))
  913.  
  914. (defun ange-ftp-hash-entry-exists-p (key tbl)
  915.   "Return whether there is an association for KEY in TABLE."
  916.   (intern-soft (ange-ftp-make-hash-key key) tbl))
  917.  
  918. (defun ange-ftp-hash-table-keys (tbl)
  919.   "Return a sorted list of all the active keys in the hashtable, as strings."
  920.   (sort (all-completions "" tbl)
  921.     (function string-lessp)))
  922.  
  923. ;;;; ------------------------------------------------------------
  924. ;;;; Internal variables.
  925. ;;;; ------------------------------------------------------------
  926.  
  927. (defconst ange-ftp-version "$Revision: 4.20 $")
  928.  
  929. (defvar ange-ftp-data-buffer-name " *ftp data*"
  930.   "Buffer name to hold directory listing data received from ftp process.")
  931.  
  932. (defvar ange-ftp-netrc-modtime nil
  933.   "Last modified time of the netrc file from file-attributes.")
  934.  
  935. (defvar ange-ftp-user-hashtable (ange-ftp-make-hashtable)
  936.   "Hash table holding associations between HOST, USER pairs.")
  937.  
  938. (defvar ange-ftp-passwd-hashtable (ange-ftp-make-hashtable)
  939.   "Mapping between a HOST, USER pair and a PASSWORD for them.")
  940.  
  941. (defvar ange-ftp-account-hashtable (ange-ftp-make-hashtable)
  942.   "Mapping between a HOST, USER pair and a ACCOUNT password for them.")
  943.  
  944. (defvar ange-ftp-files-hashtable (ange-ftp-make-hashtable 97)
  945.   "Hash table for storing directories and their respective files.")
  946.  
  947. (defvar ange-ftp-ls-cache-lsargs nil
  948.   "Last set of args used by ange-ftp-ls.")
  949.  
  950. (defvar ange-ftp-ls-cache-file nil
  951.   "Last file passed to ange-ftp-ls.")
  952.  
  953. (defvar ange-ftp-ls-cache-res nil
  954.   "Last result returned from ange-ftp-ls.")
  955.  
  956. ;; New error symbols.
  957. (put 'ftp-error 'error-conditions '(ftp-error file-error error))
  958. ;; (put 'ftp-error 'error-message "FTP error")
  959.  
  960. ;;; ------------------------------------------------------------
  961. ;;; Match-data support (stolen from Kyle I think)
  962. ;;; ------------------------------------------------------------
  963.  
  964. (defmacro ange-ftp-save-match-data (&rest body)
  965.   "Execute the BODY forms, restoring the global value of the match data.
  966. Before executing BODY, case-fold-search is locally bound to nil."
  967.   (let ((original (make-symbol "match-data"))
  968.     case-fold-search)
  969.     (list
  970.      'let (list (list original '(match-data)))
  971.      (list 'unwind-protect
  972.            (cons 'progn body)
  973.            (list 'store-match-data original)))))
  974.  
  975. (put 'ange-ftp-save-match-data 'lisp-indent-hook 0)
  976. (put 'ange-ftp-save-match-data 'edebug-form-hook '(&rest form))
  977.  
  978. ;;; ------------------------------------------------------------
  979. ;;; Enhanced message support.
  980. ;;; ------------------------------------------------------------
  981.  
  982. (defun ange-ftp-message (fmt &rest args)
  983.   "Output the given message, but truncate to the size of the minibuffer
  984. window."
  985.   (let ((msg (apply (function format) fmt args))
  986.     (max (window-width (minibuffer-window))))
  987.     (if (>= (length msg) max)
  988.     (setq msg (concat "> " (substring msg (- 3 max)))))
  989.     (message "%s" msg)))
  990.  
  991. (defun ange-ftp-abbreviate-filename (file &optional new)
  992.   "Abbreviate the given filename relative to the default-directory.  If the
  993. optional parameter NEW is given and the non-directory parts match, only return
  994. the directory part of the file."
  995.   (ange-ftp-save-match-data
  996.     (if (and default-directory
  997.          (string-match (concat "^"
  998.                    (regexp-quote default-directory)
  999.                    ".") file))
  1000.     (setq file (substring file (1- (match-end 0)))))
  1001.     (if (and new
  1002.          (string-equal (file-name-nondirectory file)
  1003.                (file-name-nondirectory new)))
  1004.     (setq file (file-name-directory file)))
  1005.     (or file "./")))
  1006.  
  1007. ;;;; ------------------------------------------------------------
  1008. ;;;; User / Host mapping support.
  1009. ;;;; ------------------------------------------------------------
  1010.  
  1011. (defun ange-ftp-set-user (host user)
  1012.   "For a given HOST, set or change the default USER."
  1013.   (interactive "sHost: \nsUser: ")
  1014.   (ange-ftp-put-hash-entry host user ange-ftp-user-hashtable))
  1015.  
  1016. (defun ange-ftp-get-user (host)
  1017.   "Given a HOST, return the default USER."
  1018.   (ange-ftp-parse-netrc)
  1019.   (let ((user (ange-ftp-get-hash-entry host ange-ftp-user-hashtable)))
  1020.     (or user
  1021.     (prog1
  1022.         (setq user
  1023.           (cond ((stringp ange-ftp-default-user)
  1024.              ;; We have a default name.  Use it.
  1025.              ange-ftp-default-user)
  1026.             (ange-ftp-default-user
  1027.              ;; Ask the user.
  1028.              (let ((enable-recursive-minibuffers t))
  1029.                (read-string (format "User for %s: " host)
  1030.                     (user-login-name))))
  1031.             ;; Default to the user's login name.
  1032.             (t
  1033.              (user-login-name))))
  1034.       (ange-ftp-set-user host user)))))
  1035.  
  1036. ;;;; ------------------------------------------------------------
  1037. ;;;; Password support.
  1038. ;;;; ------------------------------------------------------------
  1039.  
  1040. (defun ange-ftp-read-passwd (prompt &optional default)
  1041.   "Read a password from the user. Echos a . for each character typed.
  1042. End with RET, LFD, or ESC. DEL or C-h rubs out.  ^U kills line.
  1043. Optional DEFAULT is password to start with."
  1044.   (let ((pass (if default default ""))
  1045.     (c 0)
  1046.     (echo-keystrokes 0)
  1047.     (cursor-in-echo-area t))
  1048.     (while (and (/= c ?\r) (/= c ?\n) (/= c ?\e))
  1049.       (message "%s%s"
  1050.            prompt
  1051.            (make-string (length pass) ?.))
  1052.       (setq c (read-char))
  1053.       (if (= c ?\C-u)
  1054.       (setq pass "")
  1055.     (if (and (/= c ?\b) (/= c ?\177))
  1056.         (setq pass (concat pass (char-to-string c)))
  1057.       (if (> (length pass) 0)
  1058.           (setq pass (substring pass 0 -1))))))
  1059.     (ange-ftp-repaint-minibuffer)
  1060.     (substring pass 0 -1)))
  1061.  
  1062. (defmacro ange-ftp-generate-passwd-key (host user)
  1063.   (` (concat (, host) "/" (, user))))
  1064.  
  1065. (defmacro ange-ftp-lookup-passwd (host user)
  1066.   (` (ange-ftp-get-hash-entry (ange-ftp-generate-passwd-key (, host) (, user))
  1067.                   ange-ftp-passwd-hashtable)))
  1068.  
  1069. (defun ange-ftp-set-passwd (host user passwd)
  1070.   "For a given HOST and USER, set or change the associated PASSWORD."
  1071.   (interactive (list (read-string "Host: ")
  1072.              (read-string "User: ")
  1073.              (ange-ftp-read-passwd "Password: ")))
  1074.   (ange-ftp-put-hash-entry (ange-ftp-generate-passwd-key host user)
  1075.                passwd
  1076.                ange-ftp-passwd-hashtable))
  1077.  
  1078. (defun ange-ftp-get-host-with-passwd (user)
  1079.   "Given a USER, return a host we know the password for."
  1080.   (ange-ftp-parse-netrc)
  1081.   (catch 'found-one
  1082.     (ange-ftp-map-hashtable
  1083.      (function (lambda (host val)
  1084.          (if (ange-ftp-lookup-passwd host user)
  1085.              (throw 'found-one host))))
  1086.      ange-ftp-user-hashtable)
  1087.     (ange-ftp-save-match-data
  1088.       (ange-ftp-map-hashtable
  1089.        (function
  1090.     (lambda (key value)
  1091.       (if (string-match "^[^/]*\\(/\\).*$" key)
  1092.           (let ((host (substring key 0 (match-beginning 1))))
  1093.         (if (and (string-equal user (substring key (match-end 1)))
  1094.              value)
  1095.             (throw 'found-one host))))))
  1096.        ange-ftp-passwd-hashtable))
  1097.     nil))
  1098.  
  1099. (defun ange-ftp-get-passwd (host user)
  1100.   "Given a HOST and USER, return the FTP password, prompting if it was not
  1101. previously set."
  1102.   (ange-ftp-parse-netrc)
  1103.  
  1104.   ;; look up password in the hash table first; user might have overriden the
  1105.   ;; defaults.
  1106.   (cond ((ange-ftp-lookup-passwd host user))
  1107.     
  1108.     ;; see if default user and password set from the .netrc file.
  1109.     ((and (stringp ange-ftp-default-user)
  1110.           ange-ftp-default-password
  1111.           (string-equal user ange-ftp-default-user))
  1112.      ange-ftp-default-password)
  1113.     
  1114.     ;; anonymous ftp password is handled specially since there is an
  1115.     ;; unwritten rule about how that is used on the Internet.
  1116.     ((and (or (string-equal user "anonymous")
  1117.           (string-equal user "ftp"))
  1118.           ange-ftp-generate-anonymous-password)
  1119.      (if (stringp ange-ftp-generate-anonymous-password)
  1120.          ange-ftp-generate-anonymous-password
  1121.        (concat (user-login-name) "@" (system-name))))
  1122.     
  1123.     ;; see if same user has logged in to other hosts; if so then prompt
  1124.     ;; with the password that was used there.
  1125.     (t
  1126.      (let* ((other (ange-ftp-get-host-with-passwd user))
  1127.         (passwd (if other
  1128.                 
  1129.                 ;; found another machine with the same user.
  1130.                 ;; Try that account.
  1131.                 (ange-ftp-read-passwd
  1132.                  (format "passwd for %s@%s (same as %s@%s): "
  1133.                      user host user other)
  1134.                  (ange-ftp-lookup-passwd other user))
  1135.               
  1136.               ;; I give up.  Ask the user for the password.
  1137.               (ange-ftp-read-passwd
  1138.                (format "Password for %s@%s: " user host)))))
  1139.        (ange-ftp-set-passwd host user passwd)
  1140.        passwd))))
  1141.  
  1142. ;;;; ------------------------------------------------------------
  1143. ;;;; Account support
  1144. ;;;; ------------------------------------------------------------
  1145.  
  1146. ;; Account passwords must be either specified in the .netrc file, or set
  1147. ;; manually by calling ange-ftp-set-account.  For the moment, ange-ftp doesn't
  1148. ;; check to see whether the FTP process is actually prompting for an account
  1149. ;; password.
  1150.  
  1151. (defun ange-ftp-set-account (host user account)
  1152.   "For a given HOST and USER, set or change the associated ACCOUNT password."
  1153.   (interactive (list (read-string "Host: ")
  1154.              (read-string "User: ")
  1155.              (ange-ftp-read-passwd "Account password: ")))
  1156.   (ange-ftp-put-hash-entry (ange-ftp-generate-passwd-key host user)
  1157.                account
  1158.                ange-ftp-account-hashtable))
  1159.  
  1160. (defun ange-ftp-get-account (host user)
  1161.   "Given a HOST and USER, return the FTP account."
  1162.   (ange-ftp-parse-netrc)
  1163.   (or (ange-ftp-get-hash-entry (ange-ftp-generate-passwd-key host user)
  1164.                    ange-ftp-account-hashtable)
  1165.       (and (stringp ange-ftp-default-user)
  1166.        (string-equal user ange-ftp-default-user)
  1167.        ange-ftp-default-account)))
  1168.  
  1169. ;;;; ------------------------------------------------------------
  1170. ;;;; ~/.netrc support
  1171. ;;;; ------------------------------------------------------------
  1172.  
  1173. (defun ange-ftp-chase-symlinks (file)
  1174.   "Return the filename that FILENAME references, following all symbolic links."
  1175.   (let (temp)
  1176.     (while (setq temp (ange-ftp-real-file-symlink-p file))
  1177.       (setq file
  1178.         (if (file-name-absolute-p temp)
  1179.         temp
  1180.           (concat (file-name-directory file) temp)))))
  1181.   file)
  1182.  
  1183. (defun ange-ftp-parse-netrc-token (token limit)
  1184.   "Move along current line looking for the value of the TOKEN.  Valid
  1185. separators between TOKEN and its value are commas and whitespace.
  1186. Second arg LIMIT is a limit for the search."
  1187.   (if (search-forward token limit t)
  1188.       (let (beg)
  1189.     (skip-chars-forward ", \t\r\n" limit)
  1190.     (if (eq (following-char) ?\")    ;quoted token value
  1191.         (progn (forward-char 1)
  1192.            (setq beg (point))
  1193.            (skip-chars-forward "^\"" limit)
  1194.            (forward-char 1)
  1195.            (buffer-substring beg (1- (point))))
  1196.       (setq beg (point))
  1197.       (skip-chars-forward "^, \t\r\n" limit)
  1198.       (buffer-substring beg (point))))))
  1199.  
  1200. (defun ange-ftp-parse-netrc-group ()
  1201.   "Extract the values for the tokens  \`machine\', \`login\', \`password\'
  1202. and \`account\' in the current buffer.  If successful, record the information
  1203. found."
  1204.   (beginning-of-line)
  1205.   (let ((start (point))
  1206.     (end (progn (re-search-forward "machine\\|default"
  1207.                        (point-max) 'end 2) (point)))
  1208.     machine login password account)
  1209.     (goto-char start)
  1210.     (setq machine  (ange-ftp-parse-netrc-token "machine"  end)
  1211.       login    (ange-ftp-parse-netrc-token "login"    end)
  1212.       password (ange-ftp-parse-netrc-token "password" end)
  1213.       account  (ange-ftp-parse-netrc-token "account"  end))
  1214.     (if (and machine login)
  1215.     ;; found a `machine` token.
  1216.     (progn
  1217.       (ange-ftp-set-user machine login)
  1218.       (ange-ftp-set-passwd machine login password)
  1219.       (and account
  1220.            (ange-ftp-set-account machine login account)))
  1221.       (goto-char start)
  1222.       (if (search-forward "default" end t)
  1223.       ;; found a `default' token
  1224.       (progn
  1225.         (setq login    (ange-ftp-parse-netrc-token "login"    end)
  1226.           password (ange-ftp-parse-netrc-token "password" end)
  1227.           account  (ange-ftp-parse-netrc-token "account"  end))
  1228.         (and login
  1229.          (setq ange-ftp-default-user login))
  1230.         (and password
  1231.          (setq ange-ftp-default-password password))
  1232.         (and account
  1233.          (setq ange-ftp-default-account account)))))
  1234.     (goto-char end)))
  1235.  
  1236. (defun ange-ftp-parse-netrc ()
  1237.   "If ~/.netrc file exists and has the correct permissions then extract the
  1238. \`machine\', \`login\', \`password\' and \`account\' information from within."
  1239.  
  1240.   ;; We set this before actually doing it to avoid the possibility
  1241.   ;; of an infinite loop if ange-ftp-netrc-filename is an FTP file.
  1242.   (interactive)
  1243.   (let* ((file (ange-ftp-chase-symlinks
  1244.         (ange-ftp-real-expand-file-name ange-ftp-netrc-filename)))
  1245.      (attr (ange-ftp-real-file-attributes file)))
  1246.     (if (and attr            ; file exists.
  1247.          (not (equal (nth 5 attr) ange-ftp-netrc-modtime)))    ; file changed
  1248.     (ange-ftp-save-match-data
  1249.       (if (or ange-ftp-disable-netrc-security-check
  1250.           (and (eq (nth 2 attr) (user-uid)) ; Same uids.
  1251.                (string-match ".r..------" (nth 8 attr))))
  1252.           (save-excursion
  1253.         ;; we are cheating a bit here.  I'm trying to do the equivalent
  1254.         ;; of find-file on the .netrc file, but then nuke it afterwards.
  1255.         ;; with the bit of logic below we should be able to have
  1256.         ;; encrypted .netrc files.
  1257.         (set-buffer (generate-new-buffer "*ftp-.netrc*"))
  1258.         (ange-ftp-real-insert-file-contents file)
  1259.         (setq buffer-file-name file)
  1260.         (setq default-directory (file-name-directory file))
  1261.         (normal-mode t)
  1262.         (mapcar 'funcall find-file-hooks)
  1263.         (setq buffer-file-name nil)
  1264.         (goto-char (point-min))
  1265.         (while (not (eobp))
  1266.           (ange-ftp-parse-netrc-group))
  1267.         (kill-buffer (current-buffer)))
  1268.         (ange-ftp-message "%s either not owned by you or badly protected."
  1269.                   ange-ftp-netrc-filename)
  1270.         (sit-for 1))
  1271.       (setq ange-ftp-netrc-modtime (nth 5 attr))))))
  1272.  
  1273. (defun ange-ftp-generate-root-prefixes ()
  1274.   "Return a list of prefixes of the form 'user@host:' to be used when
  1275. completion is done in the root directory."
  1276.   (ange-ftp-parse-netrc)
  1277.   (ange-ftp-save-match-data
  1278.     (let (res)
  1279.       (ange-ftp-map-hashtable
  1280.        (function
  1281.     (lambda (key value)
  1282.       (if (string-match "^[^/]*\\(/\\).*$" key)
  1283.           (let ((host (substring key 0 (match-beginning 1)))
  1284.             (user (substring key (match-end 1))))
  1285.         (setq res (cons (list (concat user "@" host ":"))
  1286.                 res))))))
  1287.        ange-ftp-passwd-hashtable)
  1288.       (ange-ftp-map-hashtable
  1289.        (function (lambda (host user)
  1290.            (setq res (cons (list (concat host ":"))
  1291.                    res))))
  1292.        ange-ftp-user-hashtable)
  1293.       (or res (list nil)))))
  1294.  
  1295. ;;;; ------------------------------------------------------------
  1296. ;;;; Remote pathname syntax support.
  1297. ;;;; ------------------------------------------------------------
  1298.  
  1299. (defmacro ange-ftp-ftp-path-component (n ns path)
  1300.   "Extract the Nth ftp path component from NS."
  1301.   (` (let ((elt (nth (, n) (, ns))))
  1302.        (substring (, path) (match-beginning elt) (match-end elt)))))
  1303.  
  1304. (defvar ange-ftp-ftp-path-arg "")
  1305. (defvar ange-ftp-ftp-path-res nil)
  1306.  
  1307. (defun ange-ftp-ftp-path (path)
  1308.   "Parse PATH according to ange-ftp-path-format (which see).
  1309. Returns a list (HOST USER PATH), or nil if PATH does not match the format."
  1310.   (if (string-equal path ange-ftp-ftp-path-arg)
  1311.       ange-ftp-ftp-path-res
  1312.     (setq ange-ftp-ftp-path-arg path
  1313.       ange-ftp-ftp-path-res
  1314.       (ange-ftp-save-match-data
  1315.         (if (string-match (car ange-ftp-path-format) path)
  1316.         (let* ((ns (cdr ange-ftp-path-format))
  1317.                (host (ange-ftp-ftp-path-component 0 ns path))
  1318.                (user (ange-ftp-ftp-path-component 1 ns path))
  1319.                (path (ange-ftp-ftp-path-component 2 ns path)))
  1320.           (if (zerop (length user))
  1321.               (setq user (ange-ftp-get-user host)))
  1322.           (list host user path))
  1323.           nil)))))
  1324.  
  1325. (defun ange-ftp-replace-path-component (fullpath path)
  1326.   "Take a FULLPATH that matches according to ange-ftp-path-format and
  1327. replace the path component with PATH."
  1328.   (ange-ftp-save-match-data
  1329.     (if (string-match (car ange-ftp-path-format) fullpath)
  1330.     (let* ((ns (cdr ange-ftp-path-format))
  1331.            (elt (nth 2 ns)))
  1332.       (concat (substring fullpath 0 (match-beginning elt))
  1333.           path
  1334.           (substring fullpath (match-end elt)))))))
  1335.  
  1336. ;;;; ------------------------------------------------------------
  1337. ;;;; Miscellaneous utils.
  1338. ;;;; ------------------------------------------------------------
  1339.  
  1340. (setq ange-ftp-tmp-keymap (make-sparse-keymap))
  1341. (define-key ange-ftp-tmp-keymap "\C-m" 'exit-minibuffer)
  1342.  
  1343. (defun ange-ftp-repaint-minibuffer ()
  1344.   "Gross hack to set minibuf_message = 0, so that the contents of the
  1345. minibuffer will show."
  1346.   (if (eq (selected-window) (minibuffer-window))
  1347.       (if (fboundp 'allocate-event)
  1348.       ;; lemacs
  1349.       (let ((unread-command-event (character-to-event ?\C-m
  1350.                               (allocate-event)))
  1351.         (enable-recursive-minibuffers t))
  1352.         (read-from-minibuffer "" nil ange-ftp-tmp-keymap nil))
  1353.     ;; v18 GNU Emacs
  1354.     (let ((unread-command-char ?\C-m)
  1355.           (enable-recursive-minibuffers t))
  1356.       (read-from-minibuffer "" nil ange-ftp-tmp-keymap nil)))))
  1357.  
  1358. (defun ange-ftp-ftp-process-buffer (host user)
  1359.   "Return the name of the buffer that collects output from the ftp process
  1360. connected to the given HOST and USER pair."
  1361.   (concat "*ftp " user "@" host "*"))
  1362.  
  1363. (defun ange-ftp-error (host user msg)
  1364.   "Display the last chunk of output from the ftp process for the given HOST
  1365. USER pair, and signal an error including MSG in the text."
  1366.   (let ((cur (selected-window))
  1367.     (pop-up-windows t))
  1368.     (pop-to-buffer
  1369.      (get-buffer-create
  1370.       (ange-ftp-ftp-process-buffer host user)))
  1371.     (goto-char (point-max))
  1372.     (select-window cur))
  1373.   (signal 'ftp-error (list (format "FTP Error: %s" msg))))
  1374.  
  1375. (defun ange-ftp-set-buffer-mode ()
  1376.   "Set the correct modes for the current buffer if it is visiting a remote
  1377. file."
  1378.   (if (and (stringp buffer-file-name)
  1379.        (ange-ftp-ftp-path buffer-file-name))
  1380.       (progn
  1381.     (auto-save-mode ange-ftp-auto-save)
  1382.     (make-variable-buffer-local 'revert-buffer-function)
  1383.     (setq revert-buffer-function 'ange-ftp-revert-buffer))))
  1384.  
  1385. (defun ange-ftp-kill-ftp-process (buffer)
  1386.   "If the BUFFER's visited filename or default-directory is an ftp filename
  1387. then kill the related ftp process."
  1388.   (interactive "bKill FTP process associated with buffer: ")
  1389.   (if (null buffer)
  1390.       (setq buffer (current-buffer)))
  1391.   (let ((file (or (buffer-file-name) default-directory)))
  1392.     (if file
  1393.     (let ((parsed (ange-ftp-ftp-path (expand-file-name file))))
  1394.       (if parsed
  1395.           (let ((host (nth 0 parsed))
  1396.             (user (nth 1 parsed)))
  1397.         (kill-buffer (ange-ftp-ftp-process-buffer host user))))))))
  1398.  
  1399. (defun ange-ftp-quote-string (string)
  1400.   "Quote any characters in STRING that may confuse the ftp process."
  1401.   (apply (function concat)
  1402.      (mapcar (function
  1403.            (lambda (char)
  1404.              (if (or (<= char ? )
  1405.                  (> char ?\~)
  1406.                      (= char ?\")
  1407.                  (= char ?\\))
  1408.              (vector ?\\ char)
  1409.                (vector char))))
  1410.          string)))
  1411.  
  1412. (defun ange-ftp-barf-if-not-directory (directory)
  1413.   (or (file-directory-p directory)
  1414.       (signal 'file-error
  1415.           (list "Opening directory"
  1416.             (if (file-exists-p directory)
  1417.             "not a directory"
  1418.               "no such file or directory")
  1419.             directory))))
  1420.  
  1421. ;;;; ------------------------------------------------------------
  1422. ;;;; FTP process filter support.
  1423. ;;;; ------------------------------------------------------------
  1424.  
  1425. (defun ange-ftp-process-handle-line (line proc)
  1426.   "Look at the given LINE from the ftp process PROC.  Try to catagorize it
  1427. into one of four categories: good, skip, fatal, or unknown."
  1428.   (cond ((string-match ange-ftp-xfer-size-msgs line)
  1429.      (setq ange-ftp-xfer-size
  1430.            (ash (string-to-int (substring line
  1431.                           (match-beginning 1)
  1432.                           (match-end 1)))
  1433.             -10)))
  1434.     ((string-match ange-ftp-skip-msgs line)
  1435.      t)
  1436.     ((string-match ange-ftp-good-msgs line)
  1437.      (setq ange-ftp-process-busy nil
  1438.            ange-ftp-process-result t
  1439.            ange-ftp-process-result-line line))
  1440.     ((string-match ange-ftp-fatal-msgs line)
  1441.      (delete-process proc)
  1442.      (setq ange-ftp-process-busy nil
  1443.            ange-ftp-process-result-line line))
  1444.     ((string-match ange-ftp-multi-msgs line)
  1445.      (setq ange-ftp-process-multi-skip t))
  1446.     (ange-ftp-process-multi-skip
  1447.      t)
  1448.     (t
  1449.      (setq ange-ftp-process-busy nil
  1450.            ange-ftp-process-result-line line))))
  1451.  
  1452. (defun ange-ftp-process-log-string (proc str)
  1453.   "For a given PROCESS, log the given STRING at the end of its
  1454. associated buffer."
  1455.   (let ((old-buffer (current-buffer)))
  1456.     (unwind-protect
  1457.     (let (moving)
  1458.       (set-buffer (process-buffer proc))
  1459.       (setq moving (= (point) (process-mark proc)))
  1460.       (save-excursion
  1461.         ;; Insert the text, moving the process-marker.
  1462.         (goto-char (process-mark proc))
  1463.         (insert str)
  1464.         (set-marker (process-mark proc) (point)))
  1465.       (if moving (goto-char (process-mark proc))))
  1466.       (set-buffer old-buffer))))
  1467.  
  1468. (defun ange-ftp-set-xfer-size (host user bytes)
  1469.   "Set the size of the next FTP transfer in bytes."
  1470.   (let ((proc (ange-ftp-get-process host user)))
  1471.     (if proc
  1472.     (let ((buf (process-buffer proc)))
  1473.       (if buf
  1474.           (save-excursion
  1475.         (set-buffer buf)
  1476.         (setq ange-ftp-xfer-size (ash bytes -10))))))))
  1477.  
  1478. (defun ange-ftp-process-handle-hash (str)
  1479.   "Remove hash marks from STRING and display count so far."
  1480.   (setq str (concat (substring str 0 (match-beginning 0))
  1481.             (substring str (match-end 0)))
  1482.     ange-ftp-hash-mark-count (+ (- (match-end 0)
  1483.                        (match-beginning 0))
  1484.                     ange-ftp-hash-mark-count))
  1485.   (and ange-ftp-process-msg
  1486.        ange-ftp-process-verbose
  1487.        (not (eq (selected-window) (minibuffer-window)))
  1488.        (not (boundp 'search-message))    ;screws up isearch otherwise
  1489.        (not cursor-in-echo-area)    ;screws up y-or-n-p otherwise
  1490.        (let ((kbytes (ash (* ange-ftp-hash-mark-unit
  1491.                  ange-ftp-hash-mark-count)
  1492.               -6)))
  1493.        (if (zerop ange-ftp-xfer-size)
  1494.        (ange-ftp-message "%s...%dk" ange-ftp-process-msg kbytes)
  1495.      (let ((percent (/ (* 100 kbytes) ange-ftp-xfer-size)))
  1496.        ;; cut out the redisplay of identical %-age messages.
  1497.        (if (not (eq percent ange-ftp-last-percent))
  1498.            (progn
  1499.          (setq ange-ftp-last-percent percent)
  1500.          (ange-ftp-message "%s...%d%%" ange-ftp-process-msg percent)))))))
  1501.   str)
  1502.  
  1503. (defun ange-ftp-call-cont (cont result line)
  1504.   "Call the function specified by CONT.  CONT can be either a function or a
  1505. list of a function and some args.  The first two parameters passed to the
  1506. function will be RESULT and LINE.  The remaining args will be taken from CONT
  1507. if a list was passed."
  1508.   (if cont
  1509.       (if (and (listp cont)
  1510.            (not (eq (car cont) 'lambda)))
  1511.       (apply (car cont) result line (cdr cont))
  1512.     (funcall cont result line))))
  1513.  
  1514. (defun ange-ftp-process-filter (proc str)
  1515.   "Build up a complete line of output from the ftp PROCESS and pass it
  1516. on to ange-ftp-process-handle-line to deal with."
  1517.   (let ((buffer (process-buffer proc))
  1518.     (old-buffer (current-buffer)))
  1519.  
  1520.     ;; see if the buffer is still around... it could have been deleted.
  1521.     (if (buffer-name buffer)
  1522.     (unwind-protect
  1523.         (ange-ftp-save-match-data
  1524.           (set-buffer (process-buffer proc))
  1525.           
  1526.           ;; handle hash mark printing
  1527.           (and ange-ftp-hash-mark-unit
  1528.            ange-ftp-process-busy
  1529.            (string-match "^#+$" str)
  1530.            (setq str (ange-ftp-process-handle-hash str)))
  1531.           (ange-ftp-process-log-string proc str)
  1532.           (if ange-ftp-process-busy
  1533.           (progn
  1534.             (setq ange-ftp-process-string (concat ange-ftp-process-string
  1535.                               str))
  1536.             
  1537.             ;; if we gave an empty password to the USER command earlier
  1538.             ;; then we should send a null password now.
  1539.             (if (string-match "Password: *$" ange-ftp-process-string)
  1540.             (send-string proc "\n"))))
  1541.           (while (and ange-ftp-process-busy
  1542.               (string-match "\n" ange-ftp-process-string))
  1543.         (let ((line (substring ange-ftp-process-string
  1544.                        0
  1545.                        (match-beginning 0))))
  1546.           (setq ange-ftp-process-string (substring ange-ftp-process-string
  1547.                                (match-end 0)))
  1548.           (while (string-match "^ftp> *" line)
  1549.             (setq line (substring line (match-end 0))))
  1550.           (ange-ftp-process-handle-line line proc)))
  1551.  
  1552.           ;; has the ftp client finished?  if so then do some clean-up
  1553.           ;; actions.
  1554.           (if (not ange-ftp-process-busy)
  1555.           (progn
  1556.             ;; reset the xfer size
  1557.             (setq ange-ftp-xfer-size 0)
  1558.  
  1559.             ;; issue the "done" message since we've finished.
  1560.             (if (and ange-ftp-process-msg
  1561.                  ange-ftp-process-verbose
  1562.                  ange-ftp-process-result)
  1563.             (progn
  1564.               (ange-ftp-message "%s...done" ange-ftp-process-msg)
  1565.               (ange-ftp-repaint-minibuffer)
  1566.               (setq ange-ftp-process-msg nil)))
  1567.             
  1568.             ;; is there a continuation we should be calling?  if so,
  1569.             ;; we'd better call it, making sure we only call it once.
  1570.             (if ange-ftp-process-continue
  1571.             (let ((cont ange-ftp-process-continue))
  1572.               (setq ange-ftp-process-continue nil)
  1573.               (ange-ftp-call-cont cont
  1574.                           ange-ftp-process-result
  1575.                           ange-ftp-process-result-line))))))
  1576.       (set-buffer old-buffer)))))
  1577.  
  1578. (defun ange-ftp-process-sentinel (proc str)
  1579.   "When ftp process changes state, nuke all file-entries in cache."
  1580.   (ange-ftp-save-match-data
  1581.     (let ((name (process-name proc)))
  1582.       (if (string-match "\\*ftp \\([^@]+\\)@\\([^*]+\\)*" name)
  1583.       (let ((user (substring name (match-beginning 1) (match-end 1)))
  1584.         (host (substring name (match-beginning 2) (match-end 2))))
  1585.         (ange-ftp-wipe-file-entries host user))))
  1586.     (setq ange-ftp-ls-cache-file nil)))
  1587.  
  1588. ;;;; ------------------------------------------------------------
  1589. ;;;; Gateway support.
  1590. ;;;; ------------------------------------------------------------
  1591.  
  1592. (defun ange-ftp-use-gateway-p (host)
  1593.   "Returns whether to access this host via a normal (non-smart) gateway."
  1594.   ;; yes, I know that I could simplify the following expression, but it is
  1595.   ;; clearer (to me at least) this way.
  1596.   (and (not ange-ftp-smart-gateway)
  1597.        (ange-ftp-save-match-data
  1598.      (not (string-match ange-ftp-local-host-regexp host)))))
  1599.  
  1600. (defun ange-ftp-use-smart-gateway-p (host)
  1601.   "Returns whether to access this host via a smart gateway."
  1602.   (and ange-ftp-smart-gateway
  1603.        (ange-ftp-save-match-data
  1604.      (not (string-match ange-ftp-local-host-regexp host)))))
  1605.  
  1606.  
  1607. ;;; ------------------------------------------------------------
  1608. ;;; Temporary file location and deletion...
  1609. ;;; ------------------------------------------------------------
  1610.  
  1611. (defvar ange-ftp-tmp-name-files ())
  1612. (defvar ange-ftp-tmp-name-hashtable (ange-ftp-make-hashtable 10))
  1613. (defvar ange-ftp-pid nil)
  1614.  
  1615. (defun ange-ftp-get-pid ()
  1616.   "Half-hearted attempt to get the current process's id."
  1617.   (setq ange-ftp-pid (substring (make-temp-name "") 1)))
  1618.  
  1619. (defun ange-ftp-make-tmp-name (host)
  1620.   "This routine will return the name of a new file."
  1621.   (let* ((template (if (ange-ftp-use-gateway-p host)
  1622.                ange-ftp-gateway-tmp-name-template
  1623.              ange-ftp-tmp-name-template))
  1624.      (pid (or ange-ftp-pid (ange-ftp-get-pid)))
  1625.      (start ?a)
  1626.      file entry)
  1627.     (while 
  1628.     (progn
  1629.       (setq file (format "%s%c%s" template start pid))
  1630.       (setq entry (intern file ange-ftp-tmp-name-hashtable))
  1631.       (or (memq entry ange-ftp-tmp-name-files)
  1632.           (ange-ftp-real-file-exists-p file)))
  1633.       (if (> (setq start (1+ start)) ?z)
  1634.       (progn
  1635.         (setq template (concat template "X"))
  1636.         (setq start ?a))))
  1637.     (setq ange-ftp-tmp-name-files
  1638.       (cons entry ange-ftp-tmp-name-files))
  1639.     file))
  1640.  
  1641. (defun ange-ftp-del-tmp-name (temp)
  1642.   (setq ange-ftp-tmp-name-files
  1643.     (delq (intern temp ange-ftp-tmp-name-hashtable)
  1644.           ange-ftp-tmp-name-files))
  1645.   (condition-case ()
  1646.       (ange-ftp-real-delete-file temp)
  1647.     (error nil)))
  1648.  
  1649. ;;;; ------------------------------------------------------------
  1650. ;;;; Interactive gateway program support.
  1651. ;;;; ------------------------------------------------------------
  1652.  
  1653. (defvar ange-ftp-gwp-running t)
  1654. (defvar ange-ftp-gwp-status nil)
  1655.  
  1656. (defun ange-ftp-gwp-sentinel (proc str)
  1657.   (setq ange-ftp-gwp-running nil))
  1658.  
  1659. (defun ange-ftp-gwp-filter (proc str)
  1660.   (ange-ftp-save-match-data
  1661.     (ange-ftp-process-log-string proc str)
  1662.     (cond ((string-match "login: *$" str)
  1663.        (send-string proc
  1664.             (concat
  1665.              (let ((ange-ftp-default-user t))
  1666.                (ange-ftp-get-user ange-ftp-gateway-host))
  1667.              "\n")))
  1668.       ((string-match "Password: *$" str)
  1669.        (send-string proc
  1670.             (concat
  1671.              (ange-ftp-get-passwd ange-ftp-gateway-host
  1672.                           (ange-ftp-get-user
  1673.                            ange-ftp-gateway-host))
  1674.              "\n")))
  1675.       ((string-match ange-ftp-gateway-fatal-msgs str)
  1676.        (delete-process proc)
  1677.        (setq ange-ftp-gwp-running nil))
  1678.       ((string-match ange-ftp-gateway-prompt-pattern str)
  1679.        (setq ange-ftp-gwp-running nil
  1680.          ange-ftp-gwp-status t)))))
  1681.  
  1682. (defun ange-ftp-gwp-start (host user name args)
  1683.   "Login to the gateway machine and fire up an ftp process."
  1684.   (let* ((gw-user (ange-ftp-get-user ange-ftp-gateway-host))
  1685.      (proc (start-process name name 
  1686.                   ange-ftp-gateway-program
  1687.                   ange-ftp-gateway-host))
  1688.      (ftp (mapconcat (function identity) args " ")))
  1689.     (process-kill-without-query proc)
  1690.     (set-process-sentinel proc (function ange-ftp-gwp-sentinel))
  1691.     (set-process-filter proc (function ange-ftp-gwp-filter))
  1692.     (set-marker (process-mark proc) (point))
  1693.     (setq ange-ftp-gwp-running t
  1694.       ange-ftp-gwp-status nil)
  1695.     (ange-ftp-message "Connecting to gateway %s..." ange-ftp-gateway-host)
  1696.     (while ange-ftp-gwp-running        ;perform login sequence
  1697.       (accept-process-output proc))
  1698.     (if (not ange-ftp-gwp-status)
  1699.     (ange-ftp-error host user "unable to login to gateway"))
  1700.     (ange-ftp-message "Connecting to gateway %s...done" ange-ftp-gateway-host)
  1701.     (setq ange-ftp-gwp-running t
  1702.       ange-ftp-gwp-status nil)
  1703.     (process-send-string proc ange-ftp-gateway-setup-term-command)
  1704.     (while ange-ftp-gwp-running        ;zap ^M's and double echoing.
  1705.       (accept-process-output proc))
  1706.     (if (not ange-ftp-gwp-status)
  1707.     (ange-ftp-error host user "unable to set terminal modes on gateway"))
  1708.     (setq ange-ftp-gwp-running t
  1709.       ange-ftp-gwp-status nil)
  1710.     (process-send-string proc (concat "exec " ftp "\n")) ;spawn ftp process
  1711.     proc))
  1712.  
  1713. ;;;; ------------------------------------------------------------
  1714. ;;;; Support for sending commands to the ftp process.
  1715. ;;;; ------------------------------------------------------------
  1716.  
  1717. (defun ange-ftp-raw-send-cmd (proc cmd &optional msg cont nowait)
  1718.   "Low-level routine to send the given ftp CMD to the ftp PROCESS.
  1719. MSG is an optional message to output before and after the command.
  1720. If CONT is non-NIL then it is either a function or a list of function and
  1721. some arguments.  The function will be called when the ftp command has completed.
  1722. If CONT is NIL then this routine will return \( RESULT . LINE \) where RESULT
  1723. is whether the command was successful, and LINE is the line from the FTP
  1724. process that caused the command to complete.
  1725. If NOWAIT is given then the routine will return immediately the command has
  1726. been queued with no result.  CONT will still be called, however."
  1727.   (if (memq (process-status proc) '(run open))
  1728.       (save-excursion
  1729.     (set-buffer (process-buffer proc))
  1730.     (while ange-ftp-process-busy
  1731.       (accept-process-output))
  1732.     (setq ange-ftp-process-string ""
  1733.           ange-ftp-process-result-line ""
  1734.           ange-ftp-process-busy t
  1735.           ange-ftp-process-result nil
  1736.           ange-ftp-process-multi-skip nil
  1737.           ange-ftp-process-msg msg
  1738.           ange-ftp-process-continue cont
  1739.           ange-ftp-hash-mark-count 0
  1740.           ange-ftp-last-percent -1
  1741.           cmd (concat cmd "\n"))
  1742.     (and msg ange-ftp-process-verbose (ange-ftp-message "%s..." msg))
  1743.     (goto-char (point-max))
  1744.     (move-marker last-input-start (point))
  1745.     ;; don't insert the password into the buffer on the USER command.
  1746.     (ange-ftp-save-match-data
  1747.       (if (string-match "^user \"[^\"]*\"" cmd)
  1748.           (insert (substring cmd 0 (match-end 0)) " Turtle Power!\n")
  1749.         (insert cmd)))
  1750.     (move-marker last-input-end (point))
  1751.     (send-string proc cmd)
  1752.     (set-marker (process-mark proc) (point))
  1753.     (if nowait
  1754.         nil
  1755.       ;; hang around for command to complete
  1756.       (while ange-ftp-process-busy
  1757.         (accept-process-output proc))
  1758.       (if cont
  1759.           nil            ;cont has already been called
  1760.         (cons ange-ftp-process-result ange-ftp-process-result-line))))))
  1761.  
  1762. (defun ange-ftp-nslookup-host (host)
  1763.   "Attempt to resolve the given HOSTNAME using nslookup if possible."
  1764.   (interactive "sHost:  ")
  1765.   (if ange-ftp-nslookup-program
  1766.       (let ((proc (start-process " *nslookup*" " *nslookup*"
  1767.                  ange-ftp-nslookup-program host))
  1768.         (res host))
  1769.     (process-kill-without-query proc)
  1770.     (save-excursion
  1771.       (set-buffer (process-buffer proc))
  1772.       (while (memq (process-status proc) '(run open))
  1773.         (accept-process-output proc))
  1774.       (goto-char (point-min))
  1775.       (if (re-search-forward "Name:.*\nAddress: *\\(.*\\)$" nil t)
  1776.           (setq res (buffer-substring (match-beginning 1)
  1777.                       (match-end 1))))
  1778.       (kill-buffer (current-buffer)))
  1779.     res)
  1780.     host))
  1781.  
  1782. (defun ange-ftp-start-process (host user name)
  1783.   "Spawn a new ftp process ready to connect to machine HOST and give it NAME.
  1784. If HOST is only ftp-able through a gateway machine then spawn a shell
  1785. on the gateway machine to do the ftp instead."
  1786.   (let* ((use-gateway (ange-ftp-use-gateway-p host))
  1787.      (ftp-prog (if use-gateway 
  1788.                ange-ftp-gateway-ftp-program-name
  1789.              ange-ftp-ftp-program-name))
  1790.      (args (append (list ftp-prog) ange-ftp-ftp-program-args))
  1791.      proc)
  1792.     (if use-gateway
  1793.     (if ange-ftp-gateway-program-interactive
  1794.         (setq proc (ange-ftp-gwp-start host user name args))
  1795.       (setq proc (apply 'start-process name name
  1796.                 (append (list ange-ftp-gateway-program
  1797.                       ange-ftp-gateway-host)
  1798.                     args))))
  1799.       (setq proc (apply 'start-process name name args)))
  1800.     (process-kill-without-query proc)
  1801.     (save-excursion
  1802.       (set-buffer (process-buffer proc))
  1803.       (ange-ftp-shell-mode))
  1804.     (set-process-sentinel proc (function ange-ftp-process-sentinel))
  1805.     (set-process-filter proc (function ange-ftp-process-filter))
  1806.     (accept-process-output proc)    ;wait for ftp startup message
  1807.     proc))
  1808.  
  1809. (defun ange-ftp-smart-login (host user pass account proc)
  1810.   "Connect to the FTP-server on HOST as USER using PASSWORD and ACCOUNT.
  1811. PROC is the FTP-client's process.  This routine uses the smart-gateway
  1812. host specified in ``ange-ftp-gateway-host''."
  1813.   (let ((result (ange-ftp-raw-send-cmd
  1814.          proc
  1815.          (format "open %s %s"
  1816.              (ange-ftp-nslookup-host ange-ftp-gateway-host)
  1817.              ange-ftp-smart-gateway-port)
  1818.          (format "Opening FTP connection to %s via %s"
  1819.              host
  1820.              ange-ftp-gateway-host))))
  1821.     (or (car result)
  1822.     (ange-ftp-error host user 
  1823.             (concat "OPEN request failed: "
  1824.                 (cdr result))))
  1825.     (setq result (ange-ftp-raw-send-cmd
  1826.           proc (format "user \"%s\"@%s %s %s"
  1827.                    user
  1828.                    (ange-ftp-nslookup-host host)
  1829.                    pass
  1830.                    account)
  1831.           (format "Logging in as user %s@%s"
  1832.               user host)))
  1833.     (or (car result)
  1834.     (progn
  1835.       (ange-ftp-set-passwd host user nil) ; reset password
  1836.       (ange-ftp-set-account host user nil) ; reset account
  1837.       (ange-ftp-error host user
  1838.               (concat "USER request failed: "
  1839.                   (cdr result)))))))
  1840.  
  1841. (defun ange-ftp-normal-login (host user pass account proc)
  1842.   "Connect to the FTP-server on HOST as USER using PASSWORD and ACCOUNT.
  1843. PROC is the process to the FTP-client."
  1844.   (let ((result (ange-ftp-raw-send-cmd
  1845.          proc
  1846.          (format "open %s" (ange-ftp-nslookup-host host))
  1847.          (format "Opening FTP connection to %s" host))))
  1848.     (or (car result)
  1849.     (ange-ftp-error host user
  1850.             (concat "OPEN request failed: "
  1851.                 (cdr result))))
  1852.     (setq result (ange-ftp-raw-send-cmd
  1853.           proc
  1854.           (format "user \"%s\" %s %s" user pass account)
  1855.           (format "Logging in as user %s@%s" user host)))
  1856.     (or (car result)
  1857.     (progn
  1858.       (ange-ftp-set-passwd host user nil) ;reset password.
  1859.       (ange-ftp-set-account host user nil) ;reset account.
  1860.       (ange-ftp-error host user
  1861.               (concat "USER request failed: "
  1862.                   (cdr result)))))))
  1863.  
  1864. (defvar ange-ftp-hash-mark-msgs
  1865.   "[hH]ash mark [^0-9]*\\([0-9]+\\)"
  1866.   "*Regexp matching the FTP client's output upon doing a HASH command.")
  1867.  
  1868. (defun ange-ftp-guess-hash-mark-size (proc)
  1869.   (if ange-ftp-send-hash
  1870.       (save-excursion
  1871.     (set-buffer (process-buffer proc))
  1872.     (let* ((status (ange-ftp-raw-send-cmd proc "hash"))
  1873.            (result (car status))
  1874.            (line (cdr status)))
  1875.       (ange-ftp-save-match-data
  1876.         (if (string-match ange-ftp-hash-mark-msgs line)
  1877.         (let ((size (string-to-int
  1878.                 (substring line
  1879.                        (match-beginning 1)
  1880.                        (match-end 1)))))
  1881.           (setq ange-ftp-ascii-hash-mark-size size
  1882.             ange-ftp-hash-mark-unit (ash size -4))
  1883.  
  1884.           ;; if a default value for this is set, use that value.
  1885.           (or ange-ftp-binary-hash-mark-size
  1886.               (setq ange-ftp-binary-hash-mark-size size)))))))))
  1887.  
  1888. (defun ange-ftp-get-process (host user)
  1889.   "Return the process object for a FTP process connected to HOST and
  1890. logged in as USER.  Create a new process if needed."
  1891.   (let* ((name (ange-ftp-ftp-process-buffer host user))
  1892.      (proc (get-process name)))
  1893.     (if (and proc (memq (process-status proc) '(run open)))
  1894.     proc
  1895.       (let ((pass (ange-ftp-quote-string
  1896.            (ange-ftp-get-passwd host user)))
  1897.         (account (ange-ftp-quote-string
  1898.               (ange-ftp-get-account host user))))
  1899.     ;; grab a suitable process.
  1900.     (setq proc (ange-ftp-start-process host user name))
  1901.     
  1902.     ;; login to FTP server.
  1903.     (if (ange-ftp-use-smart-gateway-p host)
  1904.         (ange-ftp-smart-login host user pass account proc)
  1905.       (ange-ftp-normal-login host user pass account proc))
  1906.       
  1907.     ;; Tell client to send back hash-marks as progress.  It isn't usually
  1908.     ;; fatal if this command fails.
  1909.     (ange-ftp-guess-hash-mark-size proc)
  1910.  
  1911.     ;; Guess at the host type.
  1912.     (ange-ftp-guess-host-type host user)
  1913.  
  1914.     ;; Run any user-specified hooks.  Note that proc, host and user are
  1915.     ;; dynamically bound at this point.
  1916.     (run-hooks 'ange-ftp-process-startup-hook))
  1917.       proc)))
  1918.  
  1919. ;; Variables for caching host and host-type
  1920. (defvar ange-ftp-host-cache nil)
  1921. (defvar ange-ftp-host-type-cache nil)
  1922.  
  1923. ;; If ange-ftp-host-type is called with the optional user
  1924. ;; argument, it will attempt to guess the host type by connecting
  1925. ;; as user, if necessary. For efficiency, I have tried to give this
  1926. ;; optional second argument only when necessary. Have I missed any calls
  1927. ;; to ange-ftp-host-type where it should have been supplied?
  1928.  
  1929. (defun ange-ftp-host-type (host &optional user)
  1930.   "Return a symbol which represents the type of the HOST given.
  1931. If the optional argument USER is given, attempts to guess the
  1932. host-type by logging in as USER."
  1933.   (if (eq host ange-ftp-host-cache)
  1934.       ange-ftp-host-type-cache
  1935.     ;; Trigger an ftp connection, in case we need to guess at the host type.
  1936.     (if (and user (ange-ftp-get-process host user) (eq host ange-ftp-host-cache))
  1937.     ange-ftp-host-type-cache
  1938.       (setq ange-ftp-host-cache host
  1939.         ange-ftp-host-type-cache
  1940.         (cond ((ange-ftp-dumb-unix-host host)
  1941.            'dumb-unix)
  1942.           ((and (fboundp 'ange-ftp-vos-host)
  1943.             (ange-ftp-vos-host host))
  1944.            'vos)
  1945.           ((and (fboundp 'ange-ftp-vms-host)
  1946.             (ange-ftp-vms-host host))
  1947.            'vms)
  1948.           ((and (fboundp 'ange-ftp-mts-host)
  1949.             (ange-ftp-mts-host host))
  1950.            'mts)
  1951.           ((and (fboundp 'ange-ftp-cms-host)
  1952.             (ange-ftp-cms-host host))
  1953.            'cms)
  1954.           (t
  1955.            'unix))))))
  1956.  
  1957. ;; It would be nice to abstract the functions ange-ftp-TYPE-host and
  1958. ;; ange-ftp-add-TYPE-host. The trick is to abstract these functions
  1959. ;; without sacrificing speed. Also, having separate variables
  1960. ;; ange-ftp-TYPE-regexp is more user friendly then requiring the user to
  1961. ;; set an alist to indicate that a host is of a given type. Even with
  1962. ;; automatic host type recognition, setting a regexp is still a good idea
  1963. ;; (for efficiency) if you log into a particular non-UNIX host frequently.
  1964.  
  1965. (defvar ange-ftp-fix-path-func-alist nil
  1966.   "Association list of \( TYPE \. FUNC \) pairs, where FUNC is a routine
  1967. which can change a UNIX path into a path more suitable for a host of type
  1968. TYPE.")
  1969.  
  1970. (defvar ange-ftp-fix-dir-path-func-alist nil
  1971.   "Association list of \( TYPE \. FUNC \) pairs, where FUNC is a routine
  1972. which can change UNIX directory path into a directory path more suitable
  1973. for a host of type TYPE.")
  1974.  
  1975. ;; *** Perhaps the sense of this variable should be inverted, since there
  1976. ;; *** is only 1 host type that can take ls-style listing options.
  1977. (defvar ange-ftp-dumb-host-types '(dumb-unix)
  1978.   "List of host types that can't take UNIX ls-style listing options.")
  1979.  
  1980. (defun ange-ftp-send-cmd (host user cmd &optional msg cont nowait)
  1981.   "Find an ftp process connected to HOST logged in as USER and send it CMD.
  1982. MSG is an optional status message to be output before and after issuing the
  1983. command.
  1984. See the documentation for ange-ftp-raw-send-cmd for a description of CONT
  1985. and NOWAIT."
  1986.   ;; Handle conversion to remote pathname syntax and remote ls option
  1987.   ;; capability.
  1988.   (let ((cmd0 (car cmd))
  1989.     (cmd1 (nth 1 cmd))
  1990.     cmd2 cmd3 host-type fix-pathname-func)
  1991.  
  1992.     (cond
  1993.      
  1994.      ;; pwd case (We don't care what host-type.)
  1995.      ((null cmd1))
  1996.      
  1997.      ;; cmd == 'dir "remote-path" "local-path" "ls-switches"
  1998.      ((progn
  1999.        (setq cmd2 (nth 2 cmd)
  2000.          host-type (ange-ftp-host-type host user))
  2001.        ;; This will trigger an FTP login, if one doesn't exist
  2002.        (eq cmd0 'dir))
  2003.       (setq cmd1 (funcall
  2004.           (or (cdr (assq host-type ange-ftp-fix-dir-path-func-alist))
  2005.               'identity)
  2006.           cmd1)
  2007.         cmd3 (nth 3 cmd))
  2008.       ;; Need to deal with the HP-UX ftp bug. This should also allow
  2009.       ;; us to resolve symlinks to directories on SysV machines. (Sebastian will
  2010.       ;; be happy.)
  2011.       (and (eq host-type 'unix)
  2012.        (string-match "/$" cmd1)
  2013.        (not (string-match "R" cmd3))
  2014.        (setq cmd1 (concat cmd1 ".")))
  2015.       ;; If the remote ls can take switches, put them in
  2016.       (or (memq host-type ange-ftp-dumb-host-types)
  2017.       (setq cmd0 'ls
  2018.         cmd1 (format "\"%s %s\"" cmd3 cmd1))))
  2019.      
  2020.      ;; First argument is the remote pathname
  2021.      ((progn
  2022.     (setq fix-pathname-func (or (cdr (assq host-type
  2023.                            ange-ftp-fix-path-func-alist))
  2024.                     'identity))
  2025.     (memq cmd0 '(get delete mkdir rmdir cd)))
  2026.       (setq cmd1 (funcall fix-pathname-func cmd1)))
  2027.  
  2028.      ;; Second argument is the remote pathname
  2029.      ((memq cmd0 '(append put chmod))
  2030.       (setq cmd2 (funcall fix-pathname-func cmd2)))
  2031.  
  2032.      ;; Both arguments are remote pathnames
  2033.      ((eq cmd0 'rename)
  2034.       (setq cmd1 (funcall fix-pathname-func cmd1)
  2035.         cmd2 (funcall fix-pathname-func cmd2))))
  2036.       
  2037.     ;; Turn the command into one long string 
  2038.     (setq cmd0 (symbol-name cmd0))
  2039.     (setq cmd (concat cmd0
  2040.               (and cmd1 (concat " " cmd1))
  2041.               (and cmd2 (concat " " cmd2))))
  2042.  
  2043.     ;; Actually send the resulting command.
  2044.     (let (afsc-result
  2045.       afsc-line)
  2046.       (ange-ftp-raw-send-cmd
  2047.        (ange-ftp-get-process host user)
  2048.        cmd
  2049.        msg
  2050.        (list
  2051.     (function (lambda (result line host user
  2052.                   cmd msg cont nowait)
  2053.             (or cont
  2054.             (setq afsc-result result
  2055.                   afsc-line line))
  2056.             (if result
  2057.             (ange-ftp-call-cont cont result line)
  2058.               (ange-ftp-raw-send-cmd
  2059.                (ange-ftp-get-process host user)
  2060.                cmd
  2061.                msg
  2062.                (list
  2063.             (function (lambda (result line cont)
  2064.                     (or cont
  2065.                     (setq afsc-result result
  2066.                           afsc-line line))
  2067.                     (ange-ftp-call-cont cont result line)))
  2068.             cont)
  2069.                nowait))))
  2070.     host user cmd msg cont nowait)
  2071.        nowait)
  2072.  
  2073.       (if nowait
  2074.       nil
  2075.     (if cont
  2076.         nil
  2077.       (cons afsc-result afsc-line))))))
  2078.  
  2079. ;; It might be nice to message users about the host type identified,
  2080. ;; but there is so much other messaging going on, it would not be
  2081. ;; seen. No point in slowing things down just so users can read
  2082. ;; a host type message.
  2083.  
  2084. (defconst ange-ftp-cms-path-template
  2085.   (concat
  2086.    "^[-A-Z0-9$*][-A-Z0-9$*]?[-A-Z0-9$*]?[-A-Z0-9$*]?[-A-Z0-9$*]?"
  2087.    "[-A-Z0-9$*]?[-A-Z0-9$*]?[-A-Z0-9$*]?\\.[0-9][0-9][0-9A-Z]$"))
  2088. (defconst ange-ftp-vms-path-template
  2089.   "^[-A-Z0-9_$]+:\\[[-A-Z0-9_$]+\\(\\.[-A-Z0-9_$]+\\)*\\]$")
  2090. (defconst ange-ftp-mts-path-template
  2091.   "^[A-Z0-9._][A-Z0-9._][A-Z0-9._][A-Z0-9._]:$")
  2092.  
  2093. (defun ange-ftp-guess-host-type (host user)
  2094.   "Guess at the the host type of HOST by doing a pwd, and examining
  2095. the directory syntax."
  2096.   (let ((host-type (ange-ftp-host-type host))
  2097.     (key (concat host "/" user "/~")))
  2098.     (if (eq host-type 'unix)
  2099.     ;; Note that ange-ftp-host-type returns unix as the default value.
  2100.     (ange-ftp-save-match-data
  2101.       (let* ((result (ange-ftp-get-pwd host user))
  2102.          (dir (car result))
  2103.          fix-path-func)
  2104.         (cond ((null dir)
  2105.            (message "Warning! Unable to get home directory")
  2106.            (sit-for 1)
  2107.            (if (string-match
  2108.             "^450 No current working directory defined$"
  2109.             (cdr result))
  2110.                
  2111.                ;; We'll assume that if pwd bombs with this
  2112.                ;; error message, then it's CMS.
  2113.                (progn
  2114.              (ange-ftp-add-cms-host host)
  2115.              (setq ange-ftp-host-cache host
  2116.                    ange-ftp-host-type-cache 'cms))))
  2117.  
  2118.           ;; try for VMS
  2119.           ((string-match ange-ftp-vms-path-template dir)
  2120.            (ange-ftp-add-vms-host host)
  2121.            ;; The add-host functions clear the host type cache.
  2122.            ;; Therefore, need to set the cache afterwards.
  2123.            (setq ange-ftp-host-cache host
  2124.              ange-ftp-host-type-cache 'vms))
  2125.  
  2126.           ;; try for MTS
  2127.           ((string-match ange-ftp-mts-path-template dir)
  2128.            (ange-ftp-add-mts-host host)
  2129.            (setq ange-ftp-host-cache host
  2130.              ange-ftp-host-type-cache 'mts))
  2131.           
  2132.           ;; try for CMS
  2133.           ((string-match ange-ftp-cms-path-template dir)
  2134.            (ange-ftp-add-cms-host host)
  2135.            (setq ange-ftp-host-cache host
  2136.              ange-ftp-host-type-cache 'cms))
  2137.  
  2138.           ;; assume UN*X
  2139.           (t
  2140.            (setq ange-ftp-host-cache host
  2141.              ange-ftp-host-type-cache 'unix)))
  2142.  
  2143.         ;; Now that we have done a pwd, might as well put it in
  2144.         ;; the expand-dir hashtable.
  2145.         (setq fix-path-func (cdr (assq ange-ftp-host-type-cache
  2146.                        ange-ftp-fix-path-func-alist)))
  2147.         (if fix-path-func
  2148.         (setq dir (funcall fix-path-func dir 'reverse)))
  2149.         (ange-ftp-put-hash-entry key dir
  2150.                      ange-ftp-expand-dir-hashtable))))
  2151.  
  2152.     ;; In the special case of CMS make sure that know the
  2153.     ;; expansion of the home minidisk now, because we will
  2154.     ;; be doing a lot of cd's.
  2155.     (if (and (eq host-type 'cms)
  2156.          (not (ange-ftp-hash-entry-exists-p
  2157.            key ange-ftp-expand-dir-hashtable)))
  2158.     (let ((dir (car (ange-ftp-get-pwd host user))))
  2159.       (if dir
  2160.           (ange-ftp-put-hash-entry key (concat "/" dir)
  2161.                        ange-ftp-expand-dir-hashtable)
  2162.         (message "Warning! Unable to get home directory")
  2163.         (sit-for 1))))))
  2164.  
  2165.  
  2166. ;;;; ------------------------------------------------------------
  2167. ;;;; Simple FTP process shell support.
  2168. ;;;; ------------------------------------------------------------
  2169.  
  2170. (defvar ange-ftp-shell-mode-map nil)
  2171.  
  2172. (defun ange-ftp-shell-mode ()
  2173.   "Major mode for interacting with an FTP process.
  2174. Return at end of buffer sends line as input.
  2175. Return not at end copies rest of line to end and sends it.
  2176.  
  2177. The following commands imitate the usual Unix interrupt and editing
  2178. control characters:
  2179. \\{ange-ftp-shell-mode-map}
  2180. Runs ange-ftp-shell-mode-hook if not nil."
  2181.   (interactive)
  2182.   (let ((proc (get-buffer-process (current-buffer))))
  2183.     (kill-all-local-variables)
  2184.     (if (not ange-ftp-shell-mode-map)
  2185.     (progn
  2186.       (require 'shell)
  2187.       (setq ange-ftp-shell-mode-map (copy-keymap shell-mode-map))
  2188.       (define-key
  2189.         ange-ftp-shell-mode-map "\C-m" 'ange-ftp-shell-send-input)))
  2190.     (use-local-map ange-ftp-shell-mode-map)
  2191.     (setq major-mode 'ange-ftp-shell-mode)
  2192.     (setq mode-name "ange-ftp")
  2193.     (setq mode-line-process '(": %s"))
  2194.     (make-local-variable 'last-input-start)
  2195.     (setq last-input-start (make-marker))
  2196.     (make-local-variable 'last-input-end)
  2197.     (setq last-input-end (make-marker))
  2198.     (goto-char (point-max))
  2199.     (set-marker (process-mark proc) (point))
  2200.     (make-local-variable 'ange-ftp-process-string)
  2201.     (setq ange-ftp-process-string "")
  2202.     (make-local-variable 'ange-ftp-process-busy)
  2203.     (make-local-variable 'ange-ftp-process-result)
  2204.     (make-local-variable 'ange-ftp-process-msg)
  2205.     (make-local-variable 'ange-ftp-process-multi-skip)
  2206.     (make-local-variable 'ange-ftp-process-result-line)
  2207.     (make-local-variable 'ange-ftp-process-continue)
  2208.     (make-local-variable 'ange-ftp-hash-mark-count)
  2209.     (make-local-variable 'ange-ftp-binary-hash-mark-size)
  2210.     (make-local-variable 'ange-ftp-ascii-hash-mark-size)
  2211.     (make-local-variable 'ange-ftp-hash-mark-unit)
  2212.     (make-local-variable 'ange-ftp-xfer-size)
  2213.     (make-local-variable 'ange-ftp-last-percent)
  2214.     (setq ange-ftp-hash-mark-count 0)
  2215.     (setq ange-ftp-xfer-size 0)
  2216.     (setq ange-ftp-process-result-line "")
  2217.     (run-hooks 'ange-ftp-shell-mode-hook)))
  2218.  
  2219. (defun ange-ftp-shell-send-input ()
  2220.   "Send input to FTP process.
  2221. At end of buffer, sends all text after last output as input to the subshell,
  2222. including a newline inserted at the end.  When not at end, copies current line
  2223. to the end of the buffer and sends it, after first attempting to discard any
  2224. prompt at the beginning of the line."
  2225.   (interactive)
  2226.   (let ((process (get-buffer-process (current-buffer))))
  2227.     (or process
  2228.     (error "Current buffer has no process"))
  2229.     (end-of-line)
  2230.     (if (eobp)
  2231.     (progn
  2232.       (move-marker last-input-start
  2233.                (process-mark process))
  2234.       (insert ?\n)
  2235.       (move-marker last-input-end (point)))
  2236.       (beginning-of-line)
  2237.       (re-search-forward "ftp> *"
  2238.              (save-excursion (end-of-line) (point))
  2239.              t)
  2240.       (let ((copy (buffer-substring (point)
  2241.                     (progn (forward-line 1) (point)))))
  2242.     (goto-char (point-max))
  2243.     (move-marker last-input-start (point))
  2244.     (insert copy)
  2245.     (move-marker last-input-end (point))))
  2246.     (process-send-region process last-input-start last-input-end)
  2247.     (set-marker (process-mark process) (point))))
  2248.  
  2249. ;;;; ------------------------------------------------------------
  2250. ;;;; Remote file and directory listing support.
  2251. ;;;; ------------------------------------------------------------
  2252.  
  2253. (defun ange-ftp-dumb-unix-host (host)
  2254.   "Returns whether HOST's FTP server doesn't like \'ls\' or \'dir\' commands
  2255. to take switch arguments."
  2256.   (and ange-ftp-dumb-unix-host-regexp
  2257.        (ange-ftp-save-match-data
  2258.      (string-match ange-ftp-dumb-unix-host-regexp host))))
  2259.  
  2260. (defun ange-ftp-add-dumb-unix-host (host)
  2261.   "Interactively adds a given HOST to ange-ftp-dumb-unix-host-regexp."
  2262.   (interactive
  2263.    (list (read-string "Host: "
  2264.               (let ((name (or (buffer-file-name)
  2265.                       (and (eq major-mode 'dired-mode)
  2266.                        dired-directory))))
  2267.             (and name (car (ange-ftp-ftp-path name)))))))
  2268.   (if (not (ange-ftp-dumb-unix-host host))
  2269.       (setq ange-ftp-dumb-unix-host-regexp
  2270.         (concat "^" (regexp-quote host) "$"
  2271.             (and ange-ftp-dumb-unix-host-regexp "\\|")
  2272.             ange-ftp-dumb-unix-host-regexp)
  2273.         ange-ftp-host-cache nil)))
  2274.  
  2275. (defvar ange-ftp-parse-list-func-alist nil
  2276.   "Association list of \( TYPE \. FUNC \) pairs.  The FUNC is a routine
  2277. which can parse the output from a DIR listing for a host of type TYPE.")
  2278.  
  2279. ;; With no-error nil, this function returns:
  2280. ;; an error if file is not an ange-ftp-path
  2281. ;;                      (This should never happen.) 
  2282. ;; an error if either the listing is unreadable or there is an ftp error.
  2283. ;; the listing (a string), if everything works.
  2284. ;; 
  2285. ;; With no-error t, it returns:
  2286. ;; an error if not an ange-ftp-path
  2287. ;; error if listing is unreable (most likely caused by a slow connection)
  2288. ;; nil if ftp error (this is because although asking to list a nonexistent
  2289. ;;                   directory on a remote unix machine usually (except
  2290. ;;                   maybe for dumb hosts) returns an ls error, but no
  2291. ;;                   ftp error, if the same is done on a VMS machine,
  2292. ;;                   an ftp error is returned. Need to trap the error
  2293. ;;                   so we can go on and try to list the parent.)
  2294. ;; the listing, if everything works.
  2295.  
  2296. (defun ange-ftp-ls (file lsargs parse &optional no-error)
  2297.   "Return the output of an `DIR' or `ls' command done over ftp.
  2298. FILE is the full name of the remote file, LSARGS is any args to pass to the
  2299. `ls' command, and PARSE specifies that the output should be parsed and stored
  2300. away in the internal cache."
  2301.   ;; If parse is t, we assume that file is a directory. i.e. we only parse
  2302.   ;; full directory listings.
  2303.   (setq file (ange-ftp-expand-file-name file))
  2304.   (let ((parsed (ange-ftp-ftp-path file)))
  2305.     (if parsed
  2306.     (let* ((host (nth 0 parsed))
  2307.            (user (nth 1 parsed))
  2308.            (path (ange-ftp-quote-string (nth 2 parsed)))
  2309.            (key (directory-file-name file))
  2310.            (host-type (ange-ftp-host-type host user))
  2311.            (dumb (memq host-type ange-ftp-dumb-host-types))
  2312.            result
  2313.            temp
  2314.            lscmd parse-func)
  2315.       (if (string-equal path "")
  2316.           (setq path
  2317.             (ange-ftp-real-file-name-as-directory
  2318.                   (ange-ftp-expand-dir host user "~"))))
  2319.       (if (and ange-ftp-ls-cache-file
  2320.            (string-equal key ange-ftp-ls-cache-file)
  2321.            ;; Don't care about lsargs for dumb hosts.
  2322.            (or dumb (string-equal lsargs ange-ftp-ls-cache-lsargs)))
  2323.           ange-ftp-ls-cache-res
  2324.         (setq temp (ange-ftp-make-tmp-name host))
  2325.         (setq lscmd (list 'dir path temp lsargs))
  2326.         (unwind-protect
  2327.         (if (car (setq result (ange-ftp-send-cmd
  2328.                        host
  2329.                        user
  2330.                        lscmd
  2331.                        (format "Listing %s"
  2332.                            (ange-ftp-abbreviate-filename
  2333.                         file)))))
  2334.             (save-excursion
  2335.               (set-buffer (get-buffer-create
  2336.                    ange-ftp-data-buffer-name))
  2337.               (erase-buffer)
  2338.               (if (ange-ftp-real-file-readable-p temp)
  2339.               (ange-ftp-real-insert-file-contents temp)
  2340.             (sleep-for ange-ftp-retry-time)
  2341.                     ;wait for file to possibly appear
  2342.             (if (ange-ftp-real-file-readable-p temp)
  2343.                 ;; Try again.
  2344.                 (ange-ftp-real-insert-file-contents temp)
  2345.               (ange-ftp-error host user
  2346.                       (format
  2347.                        "list data file %s not readable"
  2348.                        temp))))
  2349.               (if parse
  2350.               (ange-ftp-set-files
  2351.                file
  2352.                (if (setq
  2353.                 parse-func
  2354.                 (cdr (assq host-type
  2355.                        ange-ftp-parse-list-func-alist)))
  2356.                    (funcall parse-func)
  2357.                  (ange-ftp-parse-dired-listing lsargs))))
  2358.               (setq ange-ftp-ls-cache-file key
  2359.                 ange-ftp-ls-cache-lsargs lsargs
  2360.                     ; For dumb hosts-types this is
  2361.                     ; meaningless but harmless.
  2362.                 ange-ftp-ls-cache-res (buffer-string))
  2363.               ;; (kill-buffer (current-buffer))
  2364.               ange-ftp-ls-cache-res)
  2365.           (if no-error
  2366.               nil
  2367.             (ange-ftp-error host user
  2368.                     (concat "DIR failed: " (cdr result)))))
  2369.           (ange-ftp-del-tmp-name temp))))
  2370.       (error "Should never happen. Please report. Bug ref. no.: 1"))))
  2371.  
  2372. ;;;; ------------------------------------------------------------
  2373. ;;;; Directory information caching support.
  2374. ;;;; ------------------------------------------------------------
  2375.  
  2376. (defconst ange-ftp-date-regexp
  2377.   (concat
  2378.    " \\(Jan\\|Feb\\|Mar\\|Apr\\|May\\|Jun\\|Jul\\|Aug\\|Sep\\|Oct"
  2379.    "\\|Nov\\|Dec\\) +[0-3]?[0-9] "))
  2380.  
  2381. (defvar ange-ftp-add-file-entry-alist nil
  2382.   "Association list of pairs \( TYPE \. FUNC \), where FUNC
  2383. is a function to be used to add a file entry for the OS TYPE. The
  2384. main reason for this alist is to deal with file versions in VMS.")
  2385.  
  2386. (defvar ange-ftp-delete-file-entry-alist nil
  2387.   "Association list of pairs \( TYPE \. FUNC \), where FUNC
  2388. is a function to be used to delete a file entry for the OS TYPE.
  2389. The main reason for this alist is to deal with file versions in
  2390. VMS.")
  2391.  
  2392. (defun ange-ftp-add-file-entry (path &optional dir-p)
  2393.   "Given a PATH, add the file entry for it, if its directory
  2394. info exists."
  2395.   (funcall (or (cdr (assq (ange-ftp-host-type
  2396.                (car (ange-ftp-ftp-path path)))
  2397.               ange-ftp-add-file-entry-alist))
  2398.            'ange-ftp-internal-add-file-entry)
  2399.        path dir-p)
  2400.   (setq ange-ftp-ls-cache-file nil))
  2401.  
  2402. (defun ange-ftp-delete-file-entry (path &optional dir-p)
  2403.   "Given a PATH, delete the file entry for it, if its directory
  2404. info exists."
  2405.   (funcall (or (cdr (assq (ange-ftp-host-type
  2406.                (car (ange-ftp-ftp-path path)))
  2407.               ange-ftp-delete-file-entry-alist))
  2408.            'ange-ftp-internal-delete-file-entry)
  2409.        path dir-p)
  2410.   (setq ange-ftp-ls-cache-file nil))
  2411.  
  2412. (defmacro ange-ftp-parse-filename ()
  2413.   ;;Extract the filename from the current line of a dired-like listing.
  2414.   (` (let ((eol (progn (end-of-line) (point))))
  2415.        (beginning-of-line)
  2416.        (if (re-search-forward ange-ftp-date-regexp eol t)
  2417.        (progn
  2418.          (skip-chars-forward " ")
  2419.          (skip-chars-forward "^ " eol)
  2420.          (skip-chars-forward " " eol)
  2421.          ;; We bomb on filenames starting with a space.
  2422.          (buffer-substring (point) eol))))))
  2423.   
  2424. ;; This deals with the F switch. Should also do something about
  2425. ;; unquoting names obtained with the SysV b switch and the GNU Q
  2426. ;; switch. See Sebastian's dired-get-filename.
  2427.  
  2428. (defmacro ange-ftp-ls-parser ()
  2429.   ;; Note that switches is dynamically bound.
  2430.   ;; Meant to be called by ange-ftp-parse-dired-listing
  2431.   (` (let ((tbl (ange-ftp-make-hashtable))
  2432.        (used-F (and (stringp switches)
  2433.             (string-match "F" switches)))
  2434.        file-type symlink directory file)
  2435.        (while (setq file (ange-ftp-parse-filename))
  2436.      (beginning-of-line)
  2437.      (skip-chars-forward "\t 0-9")
  2438.      (setq file-type (following-char)
  2439.            directory (eq file-type ?d))
  2440.      (if (eq file-type ?l)
  2441.          (if (string-match " -> " file)
  2442.          (setq symlink (substring file (match-end 0))
  2443.                file (substring file 0 (match-beginning 0)))
  2444.            ;; Shouldn't happen
  2445.            (setq symlink ""))
  2446.        (setq symlink nil))
  2447.      ;; Only do a costly regexp search if the F switch was used.
  2448.      (if (and used-F
  2449.           (not (string-equal file ""))
  2450.           (looking-at
  2451.            ".[-r][-w]\\([^ ]\\)[-r][-w]\\([^ ]\\)[-r][-w]\\([^ ]\\)"))
  2452.          (let ((socket (eq file-type ?s))
  2453.            (executable
  2454.             (and (not symlink) ; x bits don't mean a thing for symlinks
  2455.              (string-match "[xst]"
  2456.                        (concat
  2457.                     (buffer-substring
  2458.                      (match-beginning 1)
  2459.                      (match-end 1))
  2460.                     (buffer-substring
  2461.                      (match-beginning 2)
  2462.                      (match-end 2))
  2463.                     (buffer-substring
  2464.                      (match-beginning 3)
  2465.                      (match-end 3)))))))
  2466.            ;; Some ls's with the F switch mark symlinks with an @ (ULTRIX)
  2467.            ;; and others don't. (sigh...) Beware, that some Unix's don't
  2468.            ;; seem to believe in the F-switch
  2469.            (if (or (and symlink (string-match "@$" file))
  2470.                (and directory (string-match "/$" file))
  2471.                (and executable (string-match "*$" file))
  2472.                (and socket (string-match "=$" file)))
  2473.            (setq file (substring file 0 -1)))))
  2474.      (ange-ftp-put-hash-entry file (or symlink directory) tbl)
  2475.      (forward-line 1))
  2476.        (ange-ftp-put-hash-entry "." t tbl)
  2477.        (ange-ftp-put-hash-entry ".." t tbl)
  2478.        tbl)))
  2479.  
  2480. ;;; The dl stuff for descriptive listings
  2481.  
  2482. (defvar ange-ftp-dl-dir-regexp nil
  2483.   "Regexp matching directories which are listed in dl format. This regexp
  2484. shouldn't be anchored with a trailing $ so that it will match subdirectories
  2485. as well.")
  2486.  
  2487. (defun ange-ftp-add-dl-dir (dir)
  2488.   "Interactively adds a given directory to ange-ftp-dl-dir-regexp."
  2489.   (interactive
  2490.    (list (read-string "Directory: "
  2491.               (let ((name (or (buffer-file-name)
  2492.                       (and (eq major-mode 'dired-mode)
  2493.                        dired-directory))))
  2494.             (and name (ange-ftp-ftp-path name)
  2495.                  (file-name-directory name))))))
  2496.   (if (not (and ange-ftp-dl-dir-regexp
  2497.         (string-match ange-ftp-dl-dir-regexp dir)))
  2498.       (setq ange-ftp-dl-dir-regexp
  2499.         (concat "^" (regexp-quote dir)
  2500.             (and ange-ftp-dl-dir-regexp "\\|")
  2501.             ange-ftp-dl-dir-regexp))))
  2502.  
  2503. (defmacro ange-ftp-dl-parser ()
  2504.   ;; Parse the current buffer, which is assumed to be a descriptive
  2505.   ;; listing, and return a hashtable.
  2506.   (` (let ((tbl (ange-ftp-make-hashtable)))
  2507.        (while (not (eobp))
  2508.      (ange-ftp-put-hash-entry
  2509.       (buffer-substring (point)
  2510.                 (progn
  2511.                   (skip-chars-forward "^ /\n")
  2512.                   (point)))
  2513.       (eq (following-char) ?/)
  2514.       tbl)
  2515.      (forward-line 1))
  2516.        (ange-ftp-put-hash-entry "." t tbl)
  2517.        (ange-ftp-put-hash-entry ".." t tbl)
  2518.        tbl)))
  2519.  
  2520. (defun ange-ftp-parse-dired-listing (&optional switches)
  2521.   "Parse the current buffer which is assumed to be in a dired-like listing
  2522. format, and return a hashtable as the result. If the listing is not really
  2523. a listing, then return nil."
  2524.   (ange-ftp-save-match-data
  2525.     (cond
  2526.      ((looking-at "^total [0-9]+$")
  2527.       (forward-line 1)
  2528.       (ange-ftp-ls-parser))
  2529.      ((looking-at "[^\n]+\\( not found\\|: Not a directory\\)\n\\'")
  2530.       ;; It's an ls error message.
  2531.       nil)
  2532.      ((eobp) ; i.e. (zerop (buffer-size))
  2533.       ;; This could be one of:
  2534.       ;; (1) An Ultrix ls error message
  2535.       ;; (2) A listing with the A switch of an empty directory
  2536.       ;;     on a machine which doesn't give a total line.
  2537.       ;; (3) The twilight zone.
  2538.       ;; We'll assume (1) for now.
  2539.       nil)
  2540.      ((re-search-forward ange-ftp-date-regexp nil t)
  2541.       (beginning-of-line)
  2542.       (ange-ftp-ls-parser))
  2543.      ((re-search-forward "^[^ \n\t]+ +\\([0-9]+\\|-\\|=\\) " nil t)
  2544.       ;; It's a dl listing (I hope).
  2545.       ;; file is bound by the call to ange-ftp-ls
  2546.       (ange-ftp-add-dl-dir file)
  2547.       (beginning-of-line)
  2548.       (ange-ftp-dl-parser))
  2549.      (t nil))))
  2550.  
  2551. (defun ange-ftp-set-files (directory files)
  2552.   "For a given DIRECTORY, set or change the associated FILES hashtable."
  2553.   (and files (ange-ftp-put-hash-entry (file-name-as-directory directory)
  2554.                       files ange-ftp-files-hashtable)))
  2555.  
  2556. (defun ange-ftp-get-files (directory &optional no-error)
  2557.   "Given a given DIRECTORY, return a hashtable of file entries.
  2558. This will give an error or return nil, depending on the value of
  2559. NO-ERROR, if a listing for DIRECTORY cannot be obtained."
  2560.   (setq directory (file-name-as-directory directory)) ;normalize
  2561.   (or (ange-ftp-get-hash-entry directory ange-ftp-files-hashtable)
  2562.       (ange-ftp-save-match-data
  2563.     (and (ange-ftp-ls directory
  2564.               ;; This is an efficiency hack. We try to
  2565.               ;; anticipate what sort of listing dired
  2566.               ;; might want, and cache just such a listing.
  2567.               (if (and (boundp 'dired-actual-switches)
  2568.                    (stringp dired-actual-switches)
  2569.                    ;; We allow the A switch, which lists
  2570.                    ;; all files except "." and "..".
  2571.                    ;; This is OK because we manually
  2572.                    ;; insert these entries
  2573.                    ;; in the hash table.
  2574.                    (string-match
  2575.                     "[aA]" dired-actual-switches)
  2576.                    (string-match
  2577.                     "l" dired-actual-switches)
  2578.                    (not (string-match
  2579.                      "R" dired-actual-switches)))
  2580.                   dired-actual-switches
  2581.                 (if (and (boundp 'dired-listing-switches)
  2582.                      (stringp dired-listing-switches)
  2583.                      (string-match
  2584.                       "[aA]" dired-listing-switches)
  2585.                      (string-match
  2586.                       "l" dired-listing-switches)
  2587.                      (not (string-match
  2588.                        "R" dired-listing-switches)))
  2589.                 dired-listing-switches
  2590.                   "-al"))
  2591.               t no-error)
  2592.          (ange-ftp-get-hash-entry
  2593.           directory ange-ftp-files-hashtable)))))
  2594.  
  2595. (defmacro ange-ftp-get-file-part (path)
  2596.   "Given PATH, return the file part that can be used for looking up the
  2597. file's entry in a hashtable."
  2598.   (` (let ((file (file-name-nondirectory (, path))))
  2599.        (if (string-equal file "")
  2600.        "."
  2601.      file))))
  2602.  
  2603. (defmacro ange-ftp-allow-child-lookup (dir file)
  2604.   "Return whether ange-ftp-file-entry-p and ange-ftp-get-file-entry are
  2605. allowed to determine if PATH is a sub-directory by listing it directly,
  2606. rather than listing its parent directory. This is used for efficiency so
  2607. that a wasted listing is not done:
  2608. 1. When looking for a .dired file in dired-x.el.
  2609. 2. The syntax of FILE and DIR make it impossible that FILE could be a valid
  2610.     subdirectory. This is of course an OS dependent judgement."
  2611.   (` (not
  2612.       (let* ((efile (, file)) ; expand once.
  2613.          (edir (, dir))
  2614.          (parsed (ange-ftp-ftp-path edir))
  2615.          (host-type (ange-ftp-host-type
  2616.              (car parsed))))
  2617.     (or
  2618.      ;; Deal with dired
  2619.      (and (boundp 'dired-local-variables-file)
  2620.           (stringp dired-local-variables-file)
  2621.           (string-equal dired-local-variables-file efile))
  2622.      ;; No dots in dir names in vms.
  2623.      (and (eq host-type 'vms)
  2624.           (string-match "\\." efile))
  2625.      ;; No subdirs in mts of cms.
  2626.      (and (memq host-type '(mts cms))
  2627.           (not (string-equal "/" (nth 2 parsed)))))))))
  2628.  
  2629. (defun ange-ftp-file-entry-p (path)
  2630.   "Given PATH, return whether there is a file entry for it."
  2631.   (let* ((path (directory-file-name path))
  2632.      (dir (file-name-directory path))
  2633.      (ent (ange-ftp-get-hash-entry dir ange-ftp-files-hashtable))
  2634.      (file (ange-ftp-get-file-part path)))
  2635.     (if ent
  2636.     (ange-ftp-hash-entry-exists-p file ent)
  2637.       (or (and (ange-ftp-allow-child-lookup dir file)
  2638.            (setq ent (ange-ftp-get-files path t))
  2639.            ;; Try a child lookup. i.e. try to list file as a
  2640.            ;; subdirectory of dir. This is a good idea because
  2641.            ;; we may not have read permission for file's parent. Also,
  2642.            ;; people tend to work down directory trees anyway. We use
  2643.            ;; no-error ;; because if file does not exist as a subdir.,
  2644.            ;; then dumb hosts will give an ftp error. Smart unix hosts
  2645.            ;; will simply send back the ls
  2646.            ;; error message.
  2647.            (ange-ftp-get-hash-entry "." ent))
  2648.       ;; Child lookup failed. Try the parent. If this bombs,
  2649.       ;; we are at wits end -- signal an error.
  2650.       ;; Problem: If this signals an error, the error message
  2651.       ;; may  not have a lot to do with what went wrong.
  2652.       (ange-ftp-hash-entry-exists-p file
  2653.                     (ange-ftp-get-files dir))))))
  2654.  
  2655. (defun ange-ftp-get-file-entry (path)
  2656.   "Given PATH, return the given file entry which will be either t for a
  2657. directory, nil for a normal file, or a string for a symlink. If the file
  2658. isn't in the hashtable, this also returns nil."
  2659.   (let* ((path (directory-file-name path))
  2660.      (dir (file-name-directory path))
  2661.      (ent (ange-ftp-get-hash-entry dir ange-ftp-files-hashtable))
  2662.      (file (ange-ftp-get-file-part path)))
  2663.     (if ent
  2664.     (ange-ftp-get-hash-entry file ent)
  2665.       (or (and (ange-ftp-allow-child-lookup dir file)
  2666.            (setq ent (ange-ftp-get-files path t))
  2667.            (ange-ftp-get-hash-entry "." ent))
  2668.            ;; i.e. it's a directory by child lookup
  2669.       (ange-ftp-get-hash-entry file
  2670.                    (ange-ftp-get-files dir))))))
  2671.  
  2672. (defun ange-ftp-internal-delete-file-entry (path &optional dir-p)
  2673.   (if dir-p
  2674.       (progn 
  2675.     (setq path (file-name-as-directory path))
  2676.     (ange-ftp-del-hash-entry path ange-ftp-files-hashtable)
  2677.     (setq path (directory-file-name path))))
  2678.   ;; Note that file-name-as-directory followed by directory-file-name
  2679.   ;; serves to canonicalize directory file names to their unix form.
  2680.   ;; i.e. in VMS, FOO.DIR -> FOO/ -> FOO
  2681.   (let ((files (ange-ftp-get-hash-entry (file-name-directory path)
  2682.                     ange-ftp-files-hashtable)))
  2683.     (if files
  2684.     (ange-ftp-del-hash-entry (ange-ftp-get-file-part path)
  2685.                  files))))
  2686.  
  2687. (defun ange-ftp-internal-add-file-entry (path &optional dir-p)
  2688.   (and dir-p
  2689.        (setq path (directory-file-name path)))
  2690.   (let ((files (ange-ftp-get-hash-entry (file-name-directory path)
  2691.                     ange-ftp-files-hashtable)))
  2692.     (if files
  2693.     (ange-ftp-put-hash-entry (ange-ftp-get-file-part path)
  2694.                  dir-p
  2695.                  files))))
  2696.  
  2697. (defun ange-ftp-wipe-file-entries (host user)
  2698.   "Replace the file entry information hashtable with one that doesn't have any
  2699. entries for the given HOST, USER pair."
  2700.   (let ((new-tbl (ange-ftp-make-hashtable (length ange-ftp-files-hashtable))))
  2701.     (ange-ftp-map-hashtable
  2702.      (function
  2703.       (lambda (key val)
  2704.     (let ((parsed (ange-ftp-ftp-path key)))
  2705.       (if parsed
  2706.           (let ((h (nth 0 parsed))
  2707.             (u (nth 1 parsed)))
  2708.         (or (and (equal host h) (equal user u))
  2709.             (ange-ftp-put-hash-entry key val new-tbl)))))))
  2710.      ange-ftp-files-hashtable)
  2711.     (setq ange-ftp-files-hashtable new-tbl)))
  2712.  
  2713. ;;;; ------------------------------------------------------------
  2714. ;;;; File transfer mode support.
  2715. ;;;; ------------------------------------------------------------
  2716.  
  2717. (defun ange-ftp-set-binary-mode (host user)
  2718.   "Tell the ftp process for the given HOST & USER to switch to binary mode."
  2719.   (let ((result (ange-ftp-send-cmd host user '(type "binary"))))
  2720.     (if (not (car result))
  2721.     (ange-ftp-error host user (concat "BINARY failed: " (cdr result)))
  2722.       (save-excursion
  2723.     (set-buffer (process-buffer (ange-ftp-get-process host user)))
  2724.     (setq ange-ftp-hash-mark-unit (ash ange-ftp-binary-hash-mark-size -4))))))
  2725.  
  2726. (defun ange-ftp-set-ascii-mode (host user)
  2727.   "Tell the ftp process for the given HOST & USER to switch to ascii mode."
  2728.   (let ((result (ange-ftp-send-cmd host user '(type "ascii"))))
  2729.     (if (not (car result))
  2730.     (ange-ftp-error host user (concat "ASCII failed: " (cdr result)))
  2731.       (save-excursion
  2732.     (set-buffer (process-buffer (ange-ftp-get-process host user)))
  2733.     (setq ange-ftp-hash-mark-unit (ash ange-ftp-ascii-hash-mark-size -4))))))
  2734.  
  2735. ;;; ------------------------------------------------------------
  2736. ;;; expand-file-name and friends...
  2737. ;;; ------------------------------------------------------------
  2738.  
  2739. (defun ange-ftp-cd (host user dir)
  2740.   (let ((result (ange-ftp-send-cmd host user (list 'cd dir) "Doing CD")))
  2741.     (or (car result)
  2742.     (ange-ftp-error host user (concat "CD failed: " (cdr result))))))
  2743.  
  2744. (defun ange-ftp-get-pwd (host user)
  2745.   "Attempts to get the current working directory for the given HOST/USER pair.
  2746. Returns \( DIR . LINE \) where DIR is either the directory or NIL if not found,
  2747. and LINE is the relevant success or fail line from the FTP-client."
  2748.   (let* ((result (ange-ftp-send-cmd host user '(pwd) "Getting PWD"))
  2749.      (line (cdr result))
  2750.      dir)
  2751.     (if (car result)
  2752.     (ange-ftp-save-match-data
  2753.       (and (or (string-match "\"\\([^\"]*\\)\"" line)
  2754.            (string-match " \\([^ ]+\\) " line)) ; stone-age VMS servers!
  2755.            (setq dir (substring line
  2756.                     (match-beginning 1)
  2757.                     (match-end 1))))))
  2758.     (cons dir line)))
  2759.  
  2760. (defconst ange-ftp-expand-dir-hashtable (ange-ftp-make-hashtable))
  2761.  
  2762. (defconst ange-ftp-expand-dir-regexp "^5.0 \\([^: ]+\\):")
  2763.  
  2764. (defun ange-ftp-expand-dir (host user dir)
  2765.   "Return the result of doing a PWD in the current FTP session to machine HOST
  2766. logged in as user USER and cd'd to directory DIR."
  2767.   (let* ((host-type (ange-ftp-host-type host user))
  2768.      ;; It is more efficient to call ange-ftp-host-type
  2769.      ;; before binding res, because ange-ftp-host-type sometimes
  2770.      ;; adds to the info in the expand-dir-hashtable.
  2771.      (fix-pathname-func
  2772.       (cdr (assq host-type ange-ftp-fix-path-func-alist)))
  2773.      (key (concat host "/" user "/" dir))
  2774.      (res (ange-ftp-get-hash-entry key ange-ftp-expand-dir-hashtable)))
  2775.     (or res
  2776.     (progn
  2777.       (or
  2778.        (string-equal user "anonymous")
  2779.        (string-equal user "ftp")
  2780.        (not (eq host-type 'unix))
  2781.        (let* ((ange-ftp-good-msgs (concat ange-ftp-expand-dir-regexp
  2782.                           "\\|"
  2783.                           ange-ftp-good-msgs))
  2784.           (result (ange-ftp-send-cmd host user
  2785.                          (list 'get dir "/dev/null")
  2786.                          (format "expanding %s" dir)))
  2787.           (line (cdr result)))
  2788.          (setq res
  2789.            (if (string-match ange-ftp-expand-dir-regexp line)
  2790.                (substring line 
  2791.                   (match-beginning 1)
  2792.                   (match-end 1))))))
  2793.       (or res
  2794.           (if (string-equal dir "~")
  2795.           (setq res (car (ange-ftp-get-pwd host user)))
  2796.         (let ((home (ange-ftp-expand-dir host user "~")))
  2797.           (unwind-protect
  2798.               (and (ange-ftp-cd host user dir)
  2799.                (setq res (car (ange-ftp-get-pwd host user))))
  2800.             (ange-ftp-cd host user home)))))
  2801.       (if res
  2802.           (progn
  2803.         (if fix-pathname-func
  2804.             (setq res (funcall fix-pathname-func res 'reverse)))
  2805.         (ange-ftp-put-hash-entry
  2806.          key res ange-ftp-expand-dir-hashtable)))
  2807.       res))))
  2808.  
  2809. (defun ange-ftp-canonize-filename (n)
  2810.   "Take a string and short-circuit //, /. and /.."
  2811.   (if (string-match ".+//" n)        ;don't upset Apollo users
  2812.       (setq n (substring n (1- (match-end 0)))))
  2813.   (let ((parsed (ange-ftp-ftp-path n)))
  2814.     (if parsed
  2815.     (let ((host (car parsed))
  2816.           (user (nth 1 parsed))
  2817.           (path (nth 2 parsed)))
  2818.       
  2819.       ;; See if remote path is absolute.  If so then just expand it and
  2820.       ;; replace the path component of the overall path.
  2821.       (cond ((string-match "^/" path)
  2822.          path)
  2823.         
  2824.         ;; Path starts with ~ or ~user.  Resolve that part of the path
  2825.         ;; making it absolute then re-expand it.
  2826.         ((string-match "^~[^/]*" path)
  2827.          (let* ((tilda (substring path
  2828.                       (match-beginning 0) 
  2829.                       (match-end 0)))
  2830.             (rest (substring path (match-end 0)))
  2831.             (dir (ange-ftp-expand-dir host user tilda)))
  2832.            (if dir
  2833.                (setq path (concat dir rest))
  2834.              (error "User \"%s\" is not known" 
  2835.                 (substring tilda 1)))))
  2836.         
  2837.         ;; relative path.  Tack on homedir and re-expand.
  2838.         (t
  2839.          (let ((dir (ange-ftp-expand-dir host user "~")))
  2840.            (if dir
  2841.                (setq path (concat
  2842.                    (ange-ftp-real-file-name-as-directory dir)
  2843.                    path))
  2844.              (error "Unable to obtain CWD")))))
  2845.       
  2846.       (setq path (ange-ftp-real-expand-file-name path))
  2847.       
  2848.       ;; see if hit real expand-file-name bug...  this will probably annoy
  2849.       ;; some Apollo people.  I'll wait until they shout, however.
  2850.       (if (string-match "^//" path)
  2851.           (setq path (substring path 1)))
  2852.       
  2853.       ;; Now substitute the expanded path back into the overall filename.
  2854.       (ange-ftp-replace-path-component n path))
  2855.       
  2856.       ;; non-ange-ftp path.  Just expand normally.
  2857.       (if (eq (string-to-char n) ?/)
  2858.       (ange-ftp-real-expand-file-name n)
  2859.     (ange-ftp-real-expand-file-name
  2860.      (ange-ftp-real-file-name-nondirectory n)
  2861.      (ange-ftp-real-file-name-directory n))))))
  2862.  
  2863. (defun ange-ftp-expand-file-name (name &optional default)
  2864.   "Documented as original."
  2865.   (ange-ftp-save-match-data
  2866.     (if (eq (string-to-char name) ?/)
  2867.     (while (cond ((string-match ".+//" name) ;don't upset Apollo users
  2868.               (setq name (substring name (1- (match-end 0)))))
  2869.              ((string-match "/~" name)
  2870.               (setq name (substring name (1- (match-end 0))))))))
  2871.     (cond ((eq (string-to-char name) ?~)
  2872.        (ange-ftp-real-expand-file-name name))
  2873.       ((eq (string-to-char name) ?/)
  2874.        (ange-ftp-canonize-filename name))
  2875.       ((zerop (length name))
  2876.        (ange-ftp-canonize-filename (or default default-directory)))
  2877.       ((ange-ftp-canonize-filename
  2878.         (concat (file-name-as-directory (or default default-directory))
  2879.             name))))))
  2880.  
  2881. ;;;; ------------------------------------------------------------
  2882. ;;;; Redefinitions of standard GNU Emacs functions.
  2883. ;;;; ------------------------------------------------------------
  2884.  
  2885. (defvar ange-ftp-file-name-as-directory-alist nil
  2886.   "Association list of \( TYPE \. FUNC \) pairs, where
  2887. FUNC converts a filename to a directory name for the operating
  2888. system TYPE.")
  2889.  
  2890. (defun ange-ftp-file-name-as-directory (name)
  2891.   "Documented as original."
  2892.   (let ((parsed (ange-ftp-ftp-path name)))
  2893.     (if parsed
  2894.     (if (string-equal (nth 2 parsed) "")
  2895.         name
  2896.       (funcall (or (cdr (assq
  2897.                  (ange-ftp-host-type (car parsed))
  2898.                  ange-ftp-file-name-as-directory-alist))
  2899.                'ange-ftp-real-file-name-as-directory)
  2900.            name))
  2901.       (ange-ftp-real-file-name-as-directory name))))
  2902.      
  2903. (defun ange-ftp-file-name-directory (name)
  2904.   "Documented as original."
  2905.   (let ((parsed (ange-ftp-ftp-path name)))
  2906.     (if parsed
  2907.     (let ((path (nth 2 parsed)))
  2908.       (if (ange-ftp-save-match-data
  2909.         (string-match "^~[^/]*$" path))
  2910.           name
  2911.         (ange-ftp-replace-path-component
  2912.          name
  2913.          (ange-ftp-real-file-name-directory path))))
  2914.       (ange-ftp-real-file-name-directory name))))
  2915.  
  2916. (defun ange-ftp-file-name-nondirectory (name)
  2917.   "Documented as original."
  2918.   (let ((parsed (ange-ftp-ftp-path name)))
  2919.     (if parsed
  2920.     (let ((path (nth 2 parsed)))
  2921.       (if (ange-ftp-save-match-data
  2922.         (string-match "^~[^/]*$" path))
  2923.           ""
  2924.         (ange-ftp-real-file-name-nondirectory path)))
  2925.       (ange-ftp-real-file-name-nondirectory name))))
  2926.  
  2927. (defun ange-ftp-directory-file-name (dir)
  2928.   "Documented as original."
  2929.   (let ((parsed (ange-ftp-ftp-path dir)))
  2930.     (if parsed
  2931.     (ange-ftp-replace-path-component
  2932.        dir
  2933.        (ange-ftp-real-directory-file-name (nth 2 parsed)))
  2934.       (ange-ftp-real-directory-file-name dir))))
  2935.  
  2936. (defun ange-ftp-binary-file (file)
  2937.   "Returns whether the given FILE is to be considered as a binary file for
  2938. ftp transfers."
  2939.   (ange-ftp-save-match-data
  2940.     (string-match ange-ftp-binary-file-name-regexp file)))
  2941.  
  2942. (defun ange-ftp-write-region (start end filename &optional append visit)
  2943.   "Documented as original."
  2944.   (interactive "r\nFWrite region to file: ")
  2945.   (setq filename (expand-file-name filename))
  2946.   (let ((parsed (ange-ftp-ftp-path filename)))
  2947.     (if parsed
  2948.     (let* ((host (nth 0 parsed))
  2949.            (user (nth 1 parsed))
  2950.            (path (ange-ftp-quote-string (nth 2 parsed)))
  2951.            (temp (ange-ftp-make-tmp-name host))
  2952.            (binary (ange-ftp-binary-file filename))
  2953.            (cmd (if append 'append 'put))
  2954.            (abbr (ange-ftp-abbreviate-filename filename)))
  2955.       (unwind-protect
  2956.           (progn
  2957.         (let ((executing-macro t)
  2958.               (filename (buffer-file-name))
  2959.               (mod-p (buffer-modified-p)))
  2960.           (unwind-protect
  2961.               (ange-ftp-real-write-region start end temp nil visit)
  2962.             ;; cleanup forms
  2963.             (setq buffer-file-name filename)
  2964.             (set-buffer-modified-p mod-p)))
  2965.         (if binary
  2966.             (ange-ftp-set-binary-mode host user))
  2967.  
  2968.         ;; tell the process filter what size the transfer will be.
  2969.         (let ((attr (file-attributes temp)))
  2970.           (if attr
  2971.               (ange-ftp-set-xfer-size host user (nth 7 attr))))
  2972.  
  2973.         ;; put or append the file.
  2974.         (let ((result (ange-ftp-send-cmd host user
  2975.                          (list cmd temp path)
  2976.                          (format "Writing %s" abbr))))
  2977.           (or (car result)
  2978.               (signal 'ftp-error
  2979.                   (list
  2980.                    "Opening output file"
  2981.                    (format "FTP Error: \"%s\"" (cdr result))
  2982.                    filename)))))
  2983.         (ange-ftp-del-tmp-name temp)
  2984.         (if binary 
  2985.         (ange-ftp-set-ascii-mode host user)))
  2986.       (if (eq visit t)
  2987.           (progn
  2988.         (ange-ftp-set-buffer-mode)
  2989.         (setq buffer-file-name filename)
  2990.         (set-buffer-modified-p nil)))
  2991.       (ange-ftp-message "Wrote %s" abbr)
  2992.       (ange-ftp-add-file-entry filename))
  2993.       (ange-ftp-real-write-region start end filename append visit))))
  2994.  
  2995. (defun ange-ftp-insert-file-contents (filename &optional visit)
  2996.   "Documented as original."
  2997.   (barf-if-buffer-read-only)
  2998.   (setq filename (expand-file-name filename))
  2999.   (let ((parsed (ange-ftp-ftp-path filename)))
  3000.     (if parsed
  3001.     (progn
  3002.       (if visit
  3003.           (setq buffer-file-name filename))
  3004.       (if (or (file-exists-p filename)
  3005.           (progn
  3006.             (setq ange-ftp-ls-cache-file nil)
  3007.             (ange-ftp-del-hash-entry (file-name-directory filename)
  3008.                          ange-ftp-files-hashtable)
  3009.             (file-exists-p filename)))
  3010.           (let* ((host (nth 0 parsed))
  3011.              (user (nth 1 parsed))
  3012.              (path (ange-ftp-quote-string (nth 2 parsed)))
  3013.              (temp (ange-ftp-make-tmp-name host))
  3014.              (binary (ange-ftp-binary-file filename))
  3015.              (abbr (ange-ftp-abbreviate-filename filename))
  3016.              size)
  3017.         (unwind-protect
  3018.             (progn
  3019.               (if binary
  3020.               (ange-ftp-set-binary-mode host user))
  3021.               (let ((result (ange-ftp-send-cmd host user
  3022.                           (list 'get path temp)
  3023.                           (format "Retrieving %s" abbr))))
  3024.             (or (car result)
  3025.                 (signal 'ftp-error
  3026.                     (list
  3027.                      "Opening input file"
  3028.                      (format "FTP Error: \"%s\"" (cdr result))
  3029.                      filename))))
  3030.               (if (or (ange-ftp-real-file-readable-p temp)
  3031.                   (sleep-for ange-ftp-retry-time)
  3032.                   ;; Wait for file to hopefully appear.
  3033.                   (ange-ftp-real-file-readable-p temp))
  3034.               (setq
  3035.                size
  3036.                (nth 1 (ange-ftp-real-insert-file-contents temp
  3037.                                       visit)))
  3038.             (signal 'ftp-error
  3039.                 (list
  3040.                  "Opening input file:"
  3041.                  (format
  3042.                   "FTP Error: %s not arrived or readable"
  3043.                   filename)))))
  3044.           (if binary
  3045.               (ange-ftp-set-ascii-mode host user))
  3046.           (ange-ftp-del-tmp-name temp))
  3047.         (if visit
  3048.             (setq buffer-file-name filename))
  3049.         (list filename size))
  3050.         (signal 'file-error
  3051.             (list 
  3052.              "Opening input file"
  3053.              filename))))
  3054.       (ange-ftp-real-insert-file-contents filename visit))))
  3055.  
  3056. (defun ange-ftp-revert-buffer (arg noconfirm)
  3057.   "Revert this buffer from a remote file using ftp."
  3058.   (let ((opoint (point)))
  3059.     (cond ((null buffer-file-name)
  3060.        (error "Buffer does not seem to be associated with any file"))
  3061.       ((or noconfirm
  3062.            (yes-or-no-p (format "Revert buffer from file %s? "
  3063.                     buffer-file-name)))
  3064.        (let ((buffer-read-only nil))
  3065.          ;; Set buffer-file-name to nil
  3066.          ;; so that we don't try to lock the file.
  3067.          (let ((buffer-file-name nil))
  3068.            (unlock-buffer)
  3069.            (erase-buffer))
  3070.          (insert-file-contents buffer-file-name t))
  3071.        (goto-char (min opoint (point-max)))
  3072.        (after-find-file nil)
  3073.        t))))
  3074.  
  3075. (defun ange-ftp-expand-symlink (file dir)
  3076.   (if (file-name-absolute-p file)
  3077.       (ange-ftp-replace-path-component dir file)
  3078.     (expand-file-name file dir)))
  3079.  
  3080. (defun ange-ftp-file-symlink-p (file)
  3081.   "Documented as original."
  3082.   ;; call ange-ftp-expand-file-name rather than the normal
  3083.   ;; expand-file-name to stop loops when using a package that
  3084.   ;; redefines both file-symlink-p and expand-file-name.
  3085.   (setq file (ange-ftp-expand-file-name file))
  3086.   (if (ange-ftp-ftp-path file)
  3087.       (let ((file-ent
  3088.          (ange-ftp-get-hash-entry
  3089.           (ange-ftp-get-file-part file)
  3090.           (ange-ftp-get-files (file-name-directory file)))))
  3091.     (if (stringp file-ent)
  3092.         (if (file-name-absolute-p file-ent)
  3093.         (ange-ftp-replace-path-component
  3094.               (file-name-directory file) file-ent)
  3095.           file-ent)))
  3096.     (ange-ftp-real-file-symlink-p file)))
  3097.  
  3098. (defun ange-ftp-file-exists-p (path)
  3099.   "Documented as original."
  3100.   (setq path (expand-file-name path))
  3101.   (if (ange-ftp-ftp-path path)
  3102.       (if (ange-ftp-file-entry-p path)
  3103.       (let ((file-ent (ange-ftp-get-file-entry path)))
  3104.         (if (stringp file-ent)
  3105.         (file-exists-p
  3106.          (ange-ftp-expand-symlink file-ent
  3107.                       (file-name-directory
  3108.                        (directory-file-name path))))
  3109.           t)))
  3110.     (ange-ftp-real-file-exists-p path)))
  3111.  
  3112. (defun ange-ftp-file-directory-p (path)
  3113.   "Documented as original."
  3114.   (setq path (expand-file-name path))
  3115.   (if (ange-ftp-ftp-path path)
  3116.       ;; We do a file-name-as-directory on path here because some
  3117.       ;; machines (VMS) use a .DIR to indicate the filename associated
  3118.       ;; with a directory. This needs to be canonicalized.
  3119.       (let ((file-ent (ange-ftp-get-file-entry
  3120.                (ange-ftp-file-name-as-directory path))))
  3121.     (if (stringp file-ent)
  3122.         (file-directory-p
  3123.          (ange-ftp-expand-symlink file-ent
  3124.                       (file-name-directory
  3125.                        (directory-file-name path))))
  3126.       file-ent))
  3127.     (ange-ftp-real-file-directory-p path)))
  3128.  
  3129. (defun ange-ftp-directory-files (directory &optional full match
  3130.                        &rest v19-args)
  3131.   "Documented as original."
  3132.   (setq directory (expand-file-name directory))
  3133.   (if (ange-ftp-ftp-path directory)
  3134.       (progn
  3135.     (ange-ftp-barf-if-not-directory directory)
  3136.     (let ((tail (ange-ftp-hash-table-keys
  3137.              (ange-ftp-get-files directory)))
  3138.           files f)
  3139.       (setq directory (file-name-as-directory directory))
  3140.       (ange-ftp-save-match-data
  3141.         (while tail
  3142.           (setq f (car tail)
  3143.             tail (cdr tail))
  3144.           (if (or (not match) (string-match match f))
  3145.           (setq files
  3146.             (cons (if full (concat directory f) f) files)))))
  3147.       (nreverse files)))
  3148.     (apply 'ange-ftp-real-directory-files directory full match v19-args)))
  3149.  
  3150. (defun ange-ftp-file-attributes (file)
  3151.   "Documented as original."
  3152.   (setq file (expand-file-name file))
  3153.   (let ((parsed (ange-ftp-ftp-path file)))
  3154.     (if parsed
  3155.     (let ((part (ange-ftp-get-file-part file))
  3156.           (files (ange-ftp-get-files (file-name-directory file))))
  3157.       (if (ange-ftp-hash-entry-exists-p part files)
  3158.           (let ((host (nth 0 parsed))
  3159.             (user (nth 1 parsed))
  3160.             (path (nth 2 parsed))
  3161.             (dirp (ange-ftp-get-hash-entry part files)))
  3162.         (list (if (and (stringp dirp) (file-name-absolute-p dirp))
  3163.               (ange-ftp-expand-symlink dirp
  3164.                            (file-name-directory file))
  3165.             dirp)        ;0 file type
  3166.               -1        ;1 link count
  3167.               -1        ;2 uid
  3168.               -1        ;3 gid
  3169.               '(0 0)        ;4 atime
  3170.               '(0 0)        ;5 mtime
  3171.               '(0 0)        ;6 ctime
  3172.               -1        ;7 size
  3173.               (concat (if (stringp dirp) "l" (if dirp "d" "-"))
  3174.                   "?????????") ;8 mode
  3175.               nil        ;9 gid weird
  3176.               ;; Hack to give remote files a unique "inode number".
  3177.               ;; It's actually the sum of the characters in its name.
  3178.               (apply '+ (nconc (mapcar 'identity host)
  3179.                        (mapcar 'identity user)
  3180.                        (mapcar 'identity
  3181.                            (directory-file-name path))))
  3182.               -1        ;11 device number [v19 only]
  3183.               ))))
  3184.       (ange-ftp-real-file-attributes file))))
  3185.  
  3186. (defun ange-ftp-file-writable-p (file)
  3187.   "Documented as original."
  3188.   (setq file (expand-file-name file))
  3189.   (if (ange-ftp-ftp-path file)
  3190.       (or (file-exists-p file)        ;guess here for speed
  3191.       (file-directory-p (file-name-directory file)))
  3192.     (ange-ftp-real-file-writable-p file)))
  3193.  
  3194. (defun ange-ftp-file-readable-p (file)
  3195.   "Documented as original."
  3196.   (setq file (expand-file-name file))
  3197.   (if (ange-ftp-ftp-path file)
  3198.       (file-exists-p file)
  3199.     (ange-ftp-real-file-readable-p file)))
  3200.  
  3201. (defun ange-ftp-delete-file (file)
  3202.   "Documented as original."
  3203.   (interactive "fDelete file: ")
  3204.   (setq file (expand-file-name file))
  3205.   (let ((parsed (ange-ftp-ftp-path file)))
  3206.     (if parsed
  3207.     (let* ((host (nth 0 parsed))
  3208.            (user (nth 1 parsed))
  3209.            (path (ange-ftp-quote-string (nth 2 parsed)))
  3210.            (abbr (ange-ftp-abbreviate-filename file))
  3211.            (result (ange-ftp-send-cmd host user
  3212.                       (list 'delete path)
  3213.                       (format "Deleting %s" abbr))))
  3214.       (or (car result)
  3215.           (signal 'ftp-error
  3216.               (list
  3217.                "Removing old name"
  3218.                (format "FTP Error: \"%s\"" (cdr result))
  3219.                file)))
  3220.       (ange-ftp-delete-file-entry file))
  3221.       (ange-ftp-real-delete-file file))))
  3222.  
  3223. (defun ange-ftp-verify-visited-file-modtime (buf)
  3224.   "Documented as original."
  3225.   (let ((name (buffer-file-name buf)))
  3226.     (if (and (stringp name) (ange-ftp-ftp-path name))
  3227.     t
  3228.       (ange-ftp-real-verify-visited-file-modtime buf))))
  3229.  
  3230. (defun ange-ftp-backup-buffer ()
  3231.   "Documented as original."
  3232.   (let (parsed)
  3233.     (if (and
  3234.      (listp ange-ftp-make-backup-files)
  3235.      (stringp buffer-file-name)
  3236.      (setq parsed (ange-ftp-ftp-path buffer-file-name))
  3237.      (or
  3238.       (null ange-ftp-make-backup-files)
  3239.       (not
  3240.        (memq
  3241.         (ange-ftp-host-type
  3242.          (car parsed))
  3243.         ange-ftp-make-backup-files))))
  3244.     nil
  3245.       (ange-ftp-real-backup-buffer))))
  3246.  
  3247. ;;;; ------------------------------------------------------------
  3248. ;;;; File copying support... totally re-written 6/24/92.
  3249. ;;;; ------------------------------------------------------------
  3250.  
  3251. (defun ange-ftp-barf-or-query-if-file-exists (absname querystring interactive)
  3252.   (if (file-exists-p absname)
  3253.       (if (not interactive)
  3254.       (signal 'file-already-exists (list absname))
  3255.     (if (not (yes-or-no-p (format "File %s already exists; %s anyway? "
  3256.                       absname querystring)))
  3257.         (signal 'file-already-exists (list absname))))))
  3258.  
  3259. ;; async local copy commented out for now since I don't seem to get
  3260. ;; the process sentinel called for some processes.
  3261. ;;
  3262. ;; (defun ange-ftp-copy-file-locally (filename newname ok-if-already-exists
  3263. ;;                         keep-date cont)
  3264. ;;   "Kludge to copy a local file and call a continuation when the copy
  3265. ;; finishes."
  3266. ;;   ;; check to see if we can overwrite
  3267. ;;   (if (or (not ok-if-already-exists)
  3268. ;;       (numberp ok-if-already-exists))
  3269. ;;       (ange-ftp-barf-or-query-if-file-exists newname "copy to it" 
  3270. ;;                          (numberp ok-if-already-exists)))
  3271. ;;   (let ((proc (start-process " *copy*"
  3272. ;;                  (generate-new-buffer "*copy*")
  3273. ;;                  "cp"
  3274. ;;                  filename
  3275. ;;                  newname))
  3276. ;;     res)
  3277. ;;     (set-process-sentinel proc (function ange-ftp-copy-file-locally-sentinel))
  3278. ;;     (process-kill-without-query proc)
  3279. ;;     (save-excursion
  3280. ;;       (set-buffer (process-buffer proc))
  3281. ;;       (make-variable-buffer-local 'copy-cont)
  3282. ;;       (setq copy-cont cont))))
  3283. ;; 
  3284. ;; (defun ange-ftp-copy-file-locally-sentinel (proc status)
  3285. ;;   (save-excursion
  3286. ;;     (set-buffer (process-buffer proc))
  3287. ;;     (let ((cont copy-cont)
  3288. ;;       (result (buffer-string)))
  3289. ;;       (unwind-protect
  3290. ;;       (if (and (string-equal status "finished\n")
  3291. ;;            (zerop (length result)))
  3292. ;;           (ange-ftp-call-cont cont t nil)
  3293. ;;         (ange-ftp-call-cont cont
  3294. ;;                 nil
  3295. ;;                 (if (zerop (length result))
  3296. ;;                     (substring status 0 -1)
  3297. ;;                   (substring result 0 -1))))
  3298. ;;     (kill-buffer (current-buffer))))))
  3299.  
  3300. ;; this is the extended version of ange-ftp-copy-file-internal that works
  3301. ;; asyncronously if asked nicely.
  3302. (defun ange-ftp-copy-file-internal (filename newname ok-if-already-exists
  3303.                          keep-date &optional msg cont nowait)
  3304.   (setq filename (expand-file-name filename)
  3305.     newname (expand-file-name newname))
  3306.  
  3307.   ;; canonicalize newname if a directory.
  3308.   (if (file-directory-p newname)
  3309.       (setq newname (expand-file-name (file-name-nondirectory filename) newname)))
  3310.  
  3311.   (let ((f-parsed (ange-ftp-ftp-path filename))
  3312.     (t-parsed (ange-ftp-ftp-path newname)))
  3313.  
  3314.     ;; local file to local file copy?
  3315.     (if (and (not f-parsed) (not t-parsed))
  3316.     (progn
  3317.       (ange-ftp-real-copy-file filename newname ok-if-already-exists
  3318.                    keep-date)
  3319.       (if cont
  3320.           (ange-ftp-call-cont cont t "Copied locally")))
  3321.       ;; one or both files are remote.
  3322.       (let* ((f-host (and f-parsed (nth 0 f-parsed)))
  3323.          (f-user (and f-parsed (nth 1 f-parsed)))
  3324.          (f-path (and f-parsed (ange-ftp-quote-string (nth 2 f-parsed))))
  3325.          (f-abbr (ange-ftp-abbreviate-filename filename))
  3326.          (t-host (and t-parsed (nth 0 t-parsed)))
  3327.          (t-user (and t-parsed (nth 1 t-parsed)))
  3328.          (t-path (and t-parsed (ange-ftp-quote-string (nth 2 t-parsed))))
  3329.          (t-abbr (ange-ftp-abbreviate-filename newname filename))
  3330.          (binary (or (ange-ftp-binary-file filename)
  3331.              (ange-ftp-binary-file newname)))
  3332.          temp1
  3333.          temp2)
  3334.  
  3335.     ;; check to see if we can overwrite
  3336.     (if (or (not ok-if-already-exists)
  3337.         (numberp ok-if-already-exists))
  3338.         (ange-ftp-barf-or-query-if-file-exists newname "copy to it" 
  3339.                            (numberp ok-if-already-exists)))
  3340.  
  3341.     ;; do the copying.
  3342.     (if f-parsed
  3343.         
  3344.         ;; filename was remote.
  3345.         (progn
  3346.           (if (or (ange-ftp-use-gateway-p f-host)
  3347.               t-parsed)
  3348.           ;; have to use intermediate file if we are getting via
  3349.           ;; gateway machine or we are doing a remote to remote copy.
  3350.           (setq temp1 (ange-ftp-make-tmp-name f-host)))
  3351.           
  3352.           (if binary
  3353.           (ange-ftp-set-binary-mode f-host f-user))
  3354.  
  3355.           (ange-ftp-send-cmd
  3356.            f-host
  3357.            f-user
  3358.            (list 'get f-path (or temp1 newname))
  3359.            (or msg
  3360.            (if (and temp1 t-parsed)
  3361.                (format "Getting %s" f-abbr)
  3362.              (format "Copying %s to %s" f-abbr t-abbr)))
  3363.            (list (function ange-ftp-cf1)
  3364.              filename newname binary msg
  3365.              f-parsed f-host f-user f-path f-abbr
  3366.              t-parsed t-host t-user t-path t-abbr
  3367.              temp1 temp2 cont nowait)
  3368.            nowait))
  3369.  
  3370.       ;; filename wasn't remote.  newname must be remote.  call the
  3371.       ;; function which does the remainder of the copying work.
  3372.       (ange-ftp-cf1 t nil
  3373.             filename newname binary msg
  3374.             f-parsed f-host f-user f-path f-abbr
  3375.             t-parsed t-host t-user t-path t-abbr
  3376.             nil nil cont nowait))))))
  3377.  
  3378. ;; next part of copying routine.
  3379. (defun ange-ftp-cf1 (result line
  3380.                 filename newname binary msg
  3381.                 f-parsed f-host f-user f-path f-abbr
  3382.                 t-parsed t-host t-user t-path t-abbr
  3383.                 temp1 temp2 cont nowait)
  3384.   (if line
  3385.       ;; filename must have been remote, and we must have just done a GET.
  3386.       (unwind-protect
  3387.       (or result
  3388.           ;; GET failed for some reason.  Clean up and get out.
  3389.           (progn
  3390.         (and temp1 (ange-ftp-del-tmp-name temp1))
  3391.         (or cont
  3392.             (signal 'ftp-error (list "Opening input file"
  3393.                          (format "FTP Error: \"%s\"" line)
  3394.                          filename)))))
  3395.     ;; cleanup
  3396.     (if binary
  3397.         (ange-ftp-set-ascii-mode f-host f-user))))
  3398.  
  3399.   (if result
  3400.       ;; We now have to copy either temp1 or filename to newname.
  3401.       (if t-parsed
  3402.       
  3403.       ;; newname was remote.
  3404.       (progn
  3405.         (if (ange-ftp-use-gateway-p t-host)
  3406.         (setq temp2 (ange-ftp-make-tmp-name t-host)))
  3407.         
  3408.         ;; make sure data is moved into the right place for the
  3409.         ;; outgoing transfer.  gateway temporary files complicate
  3410.         ;; things nicely.
  3411.         (if temp1
  3412.         (if temp2
  3413.             (if (string-equal temp1 temp2)
  3414.             (setq temp1 nil)
  3415.               (ange-ftp-real-copy-file temp1 temp2 t))
  3416.           (setq temp2 temp1 temp1 nil))
  3417.           (if temp2
  3418.           (ange-ftp-real-copy-file filename temp2 t)))
  3419.         
  3420.         (if binary
  3421.         (ange-ftp-set-binary-mode t-host t-user))
  3422.  
  3423.         ;; tell the process filter what size the file is.
  3424.         (let ((attr (file-attributes (or temp2 filename))))
  3425.           (if attr
  3426.           (ange-ftp-set-xfer-size t-host t-user (nth 7 attr))))
  3427.  
  3428.         (ange-ftp-send-cmd
  3429.          t-host
  3430.          t-user
  3431.          (list 'put (or temp2 filename) t-path)
  3432.          (or msg
  3433.          (if (and temp2 f-parsed)
  3434.              (format "Putting %s" newname)
  3435.            (format "Copying %s to %s" f-abbr t-abbr)))
  3436.          (list (function ange-ftp-cf2)
  3437.            newname t-host t-user binary temp1 temp2 cont)
  3438.          nowait))
  3439.     
  3440.     ;; newname wasn't remote.
  3441.     (ange-ftp-cf2 t nil newname t-host t-user binary temp1 temp2 cont))
  3442.  
  3443.     ;; first copy failed, tell caller
  3444.     (ange-ftp-call-cont cont result line)))
  3445.  
  3446. ;; last part of copying routine.
  3447. (defun ange-ftp-cf2 (result line newname t-host t-user binary temp1 temp2 cont)
  3448.   (unwind-protect
  3449.       (if line
  3450.       ;; result from doing a local to remote copy.
  3451.       (unwind-protect
  3452.           (progn
  3453.         (or result
  3454.             (or cont
  3455.             (signal 'ftp-error
  3456.                 (list "Opening output file"
  3457.                       (format "FTP Error: \"%s\"" line)
  3458.                       newname))))
  3459.         
  3460.         (ange-ftp-add-file-entry newname))
  3461.         
  3462.         ;; cleanup.
  3463.         (if binary
  3464.         (ange-ftp-set-ascii-mode t-host t-user)))
  3465.           
  3466.     ;; newname was local.
  3467.     (if temp1
  3468.         (ange-ftp-real-copy-file temp1 newname t)))
  3469.       
  3470.     ;; clean up
  3471.     (and temp1 (ange-ftp-del-tmp-name temp1))
  3472.     (and temp2 (ange-ftp-del-tmp-name temp2))
  3473.     (ange-ftp-call-cont cont result line)))
  3474.  
  3475. (defun ange-ftp-copy-file (filename newname &optional ok-if-already-exists
  3476.                     keep-date)
  3477.   "Documented as original."
  3478.   (interactive "fCopy file: \nFCopy %s to file: \np")
  3479.   (ange-ftp-copy-file-internal filename
  3480.                    newname
  3481.                    ok-if-already-exists
  3482.                    keep-date
  3483.                    nil
  3484.                    nil
  3485.                    (interactive-p)))
  3486.  
  3487. ;;;; ------------------------------------------------------------
  3488. ;;;; File renaming support.
  3489. ;;;; ------------------------------------------------------------
  3490.  
  3491. (defun ange-ftp-rename-remote-to-remote (filename newname f-parsed t-parsed
  3492.                           binary)
  3493.   "Rename remote file FILE to remote file NEWNAME."
  3494.   (let ((f-host (nth 0 f-parsed))
  3495.     (f-user (nth 1 f-parsed))
  3496.     (t-host (nth 0 t-parsed))
  3497.     (t-user (nth 1 t-parsed)))
  3498.     (if (and (string-equal f-host t-host)
  3499.          (string-equal f-user t-user))
  3500.     (let* ((f-path (ange-ftp-quote-string (nth 2 f-parsed)))
  3501.            (t-path (ange-ftp-quote-string (nth 2 t-parsed)))
  3502.            (cmd (list 'rename f-path t-path))
  3503.            (fabbr (ange-ftp-abbreviate-filename filename))
  3504.            (nabbr (ange-ftp-abbreviate-filename newname filename))
  3505.            (result (ange-ftp-send-cmd f-host f-user cmd
  3506.                       (format "Renaming %s to %s"
  3507.                           fabbr
  3508.                           nabbr))))
  3509.       (or (car result)
  3510.           (signal 'ftp-error
  3511.               (list
  3512.                "Renaming"
  3513.                (format "FTP Error: \"%s\"" (cdr result))
  3514.                filename
  3515.                newname)))
  3516.       (ange-ftp-add-file-entry newname)
  3517.       (ange-ftp-delete-file-entry filename))
  3518.       (ange-ftp-copy-file-internal filename newname t nil)
  3519.       (delete-file filename))))
  3520.  
  3521. (defun ange-ftp-rename-local-to-remote (filename newname)
  3522.   "Rename local FILE to remote file NEWNAME."
  3523.   (let* ((fabbr (ange-ftp-abbreviate-filename filename))
  3524.      (nabbr (ange-ftp-abbreviate-filename newname filename))
  3525.      (msg (format "Renaming %s to %s" fabbr nabbr)))
  3526.     (ange-ftp-copy-file-internal filename newname t nil msg)
  3527.     (let (ange-ftp-process-verbose)
  3528.       (delete-file filename))))
  3529.  
  3530. (defun ange-ftp-rename-remote-to-local (filename newname)
  3531.   "Rename remote file FILE to local file NEWNAME."
  3532.   (let* ((fabbr (ange-ftp-abbreviate-filename filename))
  3533.      (nabbr (ange-ftp-abbreviate-filename newname filename))
  3534.      (msg (format "Renaming %s to %s" fabbr nabbr)))
  3535.     (ange-ftp-copy-file-internal filename newname t nil msg)
  3536.     (let (ange-ftp-process-verbose)
  3537.       (delete-file filename))))
  3538.  
  3539. (defun ange-ftp-rename-file (filename newname &optional ok-if-already-exists)
  3540.   "Documented as original."
  3541.   (interactive "fRename file: \nFRename %s to file: \np")
  3542.   (setq filename (expand-file-name filename))
  3543.   (setq newname (expand-file-name newname))
  3544.   (let* ((f-parsed (ange-ftp-ftp-path filename))
  3545.      (t-parsed (ange-ftp-ftp-path newname))
  3546.      (binary (if (or f-parsed t-parsed) (ange-ftp-binary-file filename))))
  3547.     (if (and (or f-parsed t-parsed)
  3548.          (or (not ok-if-already-exists)
  3549.          (numberp ok-if-already-exists)))
  3550.     (ange-ftp-barf-or-query-if-file-exists
  3551.      newname
  3552.      "rename to it"
  3553.      (numberp ok-if-already-exists)))
  3554.     (if f-parsed
  3555.     (if t-parsed
  3556.         (ange-ftp-rename-remote-to-remote filename newname f-parsed
  3557.                           t-parsed binary)
  3558.       (ange-ftp-rename-remote-to-local filename newname))
  3559.       (if t-parsed
  3560.       (ange-ftp-rename-local-to-remote filename newname)
  3561.     (ange-ftp-real-rename-file filename newname ok-if-already-exists)))))
  3562.  
  3563. ;;;; ------------------------------------------------------------
  3564. ;;;; Classic Dired support.
  3565. ;;;; ------------------------------------------------------------
  3566.  
  3567. (defvar ange-ftp-dired-host-type nil
  3568.   "The host type associated with a dired buffer. (buffer local)")
  3569. (make-variable-buffer-local 'ange-ftp-dired-host-type)
  3570.  
  3571. (defun ange-ftp-dired-readin (dirname buffer)
  3572.   "Documented as original."
  3573.   (let ((file (ange-ftp-abbreviate-filename dirname))
  3574.     (parsed (ange-ftp-ftp-path dirname)))
  3575.     (save-excursion
  3576.       (ange-ftp-message "Reading directory %s..." file)
  3577.       (set-buffer buffer)
  3578.       (let ((buffer-read-only nil))
  3579.     (widen)
  3580.     (erase-buffer)
  3581.     (setq dirname (expand-file-name dirname))
  3582.     (if parsed
  3583.         (let ((host-type (ange-ftp-host-type (car parsed))))
  3584.           (setq ange-ftp-dired-host-type host-type)
  3585.           (insert (ange-ftp-ls dirname dired-listing-switches t)))
  3586.       (if (ange-ftp-real-file-directory-p dirname)
  3587.           (call-process "ls" nil buffer nil
  3588.                 dired-listing-switches dirname)
  3589.         (let ((default-directory
  3590.             (ange-ftp-real-file-name-directory dirname)))
  3591.           (call-process
  3592.            shell-file-name nil buffer nil
  3593.            "-c" (concat
  3594.              "ls " dired-listing-switches " "
  3595.              (ange-ftp-real-file-name-nondirectory dirname))))))
  3596.     (goto-char (point-min))
  3597.     (while (not (eobp))
  3598.       (insert "  ")
  3599.       (forward-line 1))
  3600.     (goto-char (point-min))))
  3601.     (ange-ftp-message "Reading directory %s...done" file)))
  3602.  
  3603. (defun ange-ftp-dired-revert (&optional arg noconfirm)
  3604.   "Documented as original."
  3605.   (if (and dired-directory
  3606.        (ange-ftp-ftp-path (expand-file-name dired-directory)))
  3607.       (setq ange-ftp-ls-cache-file nil))
  3608.   (ange-ftp-real-dired-revert arg noconfirm))
  3609.  
  3610. ;;;; ------------------------------------------------------------
  3611. ;;;; Tree Dired support (ange & Sebastian Kremer)
  3612. ;;;; ------------------------------------------------------------
  3613.  
  3614. (defvar ange-ftp-dired-re-exe-alist nil
  3615.   "Association list of regexps \(strings\) which match file lines of
  3616.  executable files.")
  3617.  
  3618. (defvar ange-ftp-dired-re-dir-alist nil
  3619.   "Association list of regexps \(strings\) which match file lines of
  3620.  subdirectories.")
  3621.  
  3622. (defvar ange-ftp-dired-insert-headerline-alist nil
  3623.   "Association list of \(TYPE \. FUNC \) pairs, where FUNC is
  3624. the function to be used by dired to insert the headerline of
  3625. the dired buffer.")
  3626.  
  3627. (defvar ange-ftp-dired-move-to-filename-alist nil
  3628.   "Association list of \(TYPE \. FUNC \) pairs, where FUNC is
  3629. the function to be used by dired to move to the beginning of a
  3630. filename.")
  3631.  
  3632. (defvar ange-ftp-dired-move-to-end-of-filename-alist nil
  3633.   "Association list of \(TYPE \. FUNC \) pairs, where FUNC is
  3634. the function to be used by dired to move to the end of a
  3635. filename.")
  3636.  
  3637. (defvar ange-ftp-dired-get-filename-alist nil
  3638.   "Association list of \(TYPE \. FUNC \) pairs, where FUNC is
  3639. the function to be used by dired to get a filename from the
  3640. current line.")
  3641.  
  3642. (defvar ange-ftp-dired-between-files-alist nil
  3643.   "Association list of \(TYPE \. FUNC \) pairs, where FUNC is
  3644. the function to be used by dired to determine when the point
  3645. is on a line between files.")
  3646.  
  3647. (defvar ange-ftp-dired-ls-trim-alist nil
  3648.   "Association list of \( TYPE \. FUNC \) pairs, where FUNC is
  3649. a function which trims extraneous lines from a directory listing.")
  3650.  
  3651. (defvar ange-ftp-dired-clean-directory-alist nil
  3652.   "Association list of \( TYPE \. FUNC \) pairs, where FUNC is
  3653. a function which cleans out old versions of files in the OS TYPE.")
  3654.  
  3655. (defvar ange-ftp-dired-flag-backup-files-alist nil
  3656.   "Association list of \( TYPE \. FUNC \) pairs, where FUNC is
  3657. a functions which flags the backup files for deletion in the OS TYPE.")
  3658.  
  3659. (defvar ange-ftp-dired-backup-diff-alist nil
  3660.   "Association list of \( TYPE \. FUNC \) pairs, where FUNC diffs
  3661. a file with its backup. The backup file is determined according to
  3662. the OS TYPE.")
  3663.  
  3664. ;; Could use dired-before-readin-hook here, instead of overloading
  3665. ;; dired-readin. However, if people change this hook after ange-ftp
  3666. ;; is loaded, they'll break things.
  3667. ;; Also, why overload dired-readin rather than dired-mode?
  3668. ;; Because I don't want to muck up virtual dired (see dired-x.el).
  3669.  
  3670. (defun ange-ftp-tree-dired-readin (dirname buffer)
  3671.   "Documented as original."
  3672.   (let ((parsed (ange-ftp-ftp-path dirname)))
  3673.     (if parsed
  3674.     (save-excursion
  3675.       (set-buffer buffer)
  3676.       (setq ange-ftp-dired-host-type
  3677.         (ange-ftp-host-type (car parsed)))
  3678.       (and ange-ftp-dl-dir-regexp
  3679.            (eq ange-ftp-dired-host-type 'unix)
  3680.            (string-match ange-ftp-dl-dir-regexp dirname)
  3681.            (setq ange-ftp-dired-host-type 'unix:dl))
  3682.       (let ((eentry (assq ange-ftp-dired-host-type
  3683.                   ange-ftp-dired-re-exe-alist))
  3684.         (dentry (assq ange-ftp-dired-host-type
  3685.                   ange-ftp-dired-re-dir-alist)))
  3686.         (if eentry
  3687.         (set (make-local-variable 'dired-re-exe) (cdr eentry)))
  3688.         (if dentry
  3689.         (set (make-local-variable 'dired-re-dir) (cdr dentry)))
  3690.         ;; No switches are sent to dumb hosts, so don't confuse dired.
  3691.         ;; I hope that dired doesn't get excited if it doesn't see the l
  3692.         ;; switch. If it does, then maybe fake things by setting this to
  3693.         ;; "-Al".
  3694.         (if (memq ange-ftp-dired-host-type ange-ftp-dumb-host-types)
  3695.         (setq dired-actual-switches "-Al"))))))
  3696.   (ange-ftp-real-dired-readin dirname buffer))
  3697.  
  3698. (defun ange-ftp-dired-insert-headerline (dir)
  3699.   "Documented as original."
  3700.   (funcall (or (and ange-ftp-dired-host-type
  3701.             (cdr (assq ange-ftp-dired-host-type
  3702.                    ange-ftp-dired-insert-headerline-alist)))
  3703.            'ange-ftp-real-dired-insert-headerline)
  3704.        dir))
  3705.  
  3706. (defun ange-ftp-dired-move-to-filename (&optional raise-error eol)
  3707.   "Documented as original."
  3708.   (funcall (or (and ange-ftp-dired-host-type
  3709.             (cdr (assq ange-ftp-dired-host-type
  3710.                    ange-ftp-dired-move-to-filename-alist)))
  3711.            'ange-ftp-real-dired-move-to-filename)
  3712.        raise-error eol))
  3713.  
  3714. (defun ange-ftp-dired-move-to-end-of-filename (&optional no-error)
  3715.   "Documented as original."
  3716.   (funcall (or (and ange-ftp-dired-host-type
  3717.             (cdr (assq ange-ftp-dired-host-type
  3718.                    ange-ftp-dired-move-to-end-of-filename-alist)))
  3719.            'ange-ftp-real-dired-move-to-end-of-filename)
  3720.        no-error))
  3721.  
  3722. (defun ange-ftp-dired-get-filename (&optional localp no-error-if-not-filep)
  3723.   "Documented as original."
  3724.   (funcall (or (and ange-ftp-dired-host-type
  3725.             (cdr (assq ange-ftp-dired-host-type
  3726.                    ange-ftp-dired-get-filename-alist)))
  3727.            'ange-ftp-real-dired-get-filename)
  3728.        localp no-error-if-not-filep))
  3729.  
  3730. (defun ange-ftp-dired-between-files ()
  3731.   "Documented as original."
  3732.   (funcall (or (and ange-ftp-dired-host-type
  3733.             (cdr (assq ange-ftp-dired-host-type
  3734.                    ange-ftp-dired-between-files-alist)))
  3735.            'ange-ftp-real-dired-between-files)))
  3736.  
  3737. (defvar ange-ftp-bob-version-alist nil
  3738.   "Association list of pairs \( TYPE \. FUNC \), where FUNC is
  3739. a function to be used to bob the version number off of a filename
  3740. in OS TYPE.")
  3741.  
  3742. (defun ange-ftp-dired-find-file ()
  3743.   "Documented as original."
  3744.   (interactive)
  3745.   (find-file (funcall (or (and ange-ftp-dired-host-type
  3746.                    (cdr (assq ange-ftp-dired-host-type
  3747.                       ange-ftp-bob-version-alist)))
  3748.               'identity)
  3749.               (dired-get-filename))))
  3750.  
  3751. ;; Need the following functions for making filenames of compressed
  3752. ;; files, because some OS's (unlike UNIX) do not allow a filename to
  3753. ;; have two extensions.
  3754.  
  3755. (defvar ange-ftp-dired-compress-make-compressed-filename-alist nil
  3756.   "Association list of \( TYPE \. FUNC \) pairs, where FUNC converts a
  3757. filename to the filename of the associated compressed file.")
  3758.  
  3759. (defun ange-ftp-dired-compress-make-compressed-filename (name &optional reverse)
  3760.   "Converts a filename to the filename of the associated compressed
  3761. file. With an optional reverse argument, the reverse conversion is done."
  3762.   (let ((parsed (ange-ftp-ftp-path name))
  3763.     conversion-func)
  3764.     (if (and parsed
  3765.          (setq conversion-func
  3766.            (cdr (assq (ange-ftp-host-type (car parsed))
  3767.                   ange-ftp-dired-compress-make-compressed-filename-alist))))
  3768.     (funcall conversion-func name reverse)
  3769.       (if reverse
  3770.       (if (string-match "\\.Z$" name)
  3771.           (substring name 0 (match-beginning 0))
  3772.         name)
  3773.     (concat name ".Z")))))
  3774.  
  3775. (defun ange-ftp-dired-clean-directory (keep)
  3776.   "Documented as original."
  3777.   (interactive "P")
  3778.   (funcall (or (and ange-ftp-dired-host-type
  3779.             (cdr (assq ange-ftp-dired-host-type
  3780.                    ange-ftp-dired-clean-directory-alist)))
  3781.            'ange-ftp-real-dired-clean-directory)
  3782.        keep))
  3783.  
  3784. (defun ange-ftp-dired-backup-diff (&optional switches)
  3785.   "Documented as original."
  3786.   (interactive (list (if (fboundp 'diff-read-switches)
  3787.              (diff-read-switches "Diff with switches: "))))
  3788.   (funcall (or (and ange-ftp-dired-host-type
  3789.             (cdr (assq ange-ftp-dired-host-type
  3790.                    ange-ftp-dired-backup-diff-alist)))
  3791.            'ange-ftp-real-dired-backup-diff)
  3792.        switches))
  3793.  
  3794.  
  3795. (defun ange-ftp-dired-fixup-subdirs (start file)
  3796.   "Turn each subdir name into a valid ange-ftp filename."
  3797.  
  3798.   ;; We haven't indented the listing yet.
  3799.   ;; Must be careful about filelines ending in a colon: exclude spaces!
  3800.   (let ((subdir-regexp "^\\([^ \n\r]+\\)\\(:\\)[\n\r]"))
  3801.     (save-restriction
  3802.       (save-excursion
  3803.     (narrow-to-region start (point))
  3804.     (goto-char start)
  3805.     (while (re-search-forward subdir-regexp nil t)
  3806.       (goto-char (match-beginning 1))
  3807.       (let ((name (buffer-substring (point)
  3808.                     (match-end 1))))
  3809.         (delete-region (point) (match-end 1))
  3810.         (insert (ange-ftp-replace-path-component
  3811.              file
  3812.              name))))))))
  3813.  
  3814. (defun ange-ftp-dired-ls (file switches &optional wildcard full-directory-p)
  3815.   "Documented as original."
  3816.   (let ((parsed (ange-ftp-ftp-path file)))
  3817.     (if parsed
  3818.     (let* ((pt (point))
  3819.            (path (nth 2 parsed))
  3820.            (host-type (ange-ftp-host-type (car parsed)))
  3821.            (dumb (memq host-type ange-ftp-dumb-host-types))
  3822.            trim-func case-fold-search)
  3823.       ;; Make sure that case-fold-search is nil
  3824.       ;; so that we can look at the switches.
  3825.       (if wildcard
  3826.           (if (not (memq host-type '(unix dumb-unix)))
  3827.           (insert (ange-ftp-ls file switches nil))
  3828.         ;; Prevent ls from inserting subdirs, as the subdir header
  3829.         ;; line format would be wrong (it would have no "/user@host:"
  3830.         ;; prefix)
  3831.         (insert (ange-ftp-ls file (concat switches "d") nil))
  3832.  
  3833.         ;; Quoting the path part of the file name seems to be a good
  3834.         ;; idea (using dired.el's shell-quote function), but ftpd
  3835.         ;; always globs ls args before passing them to /bin/ls or even
  3836.         ;; doing the ls formatting itself.  --> So wildcard characters
  3837.         ;; in FILE lose.  Sigh...
  3838.  
  3839.         ;; When using wildcards, some ftpd's put the whole directory
  3840.         ;; name in front of each filename.  Walk down the listing
  3841.         ;; generated and remove this stuff.
  3842.         (let ((dir (ange-ftp-real-file-name-directory path)))
  3843.           (if dir
  3844.               (let ((dirq (regexp-quote dir)))
  3845.             (save-restriction
  3846.               (save-excursion
  3847.                 (narrow-to-region pt (point))
  3848.                 (goto-char pt)
  3849.                 (while (not (eobp))
  3850.                   (if (dired-move-to-filename)
  3851.                   (if (re-search-forward dirq nil t)
  3852.                       (replace-match "")))
  3853.                   (forward-line 1))))))))
  3854.  
  3855.                ;;;;;;;;;;;;;;;;;;;;;;;;;;
  3856.                ;; Big issue here Andy! ;;
  3857.            ;;;;;;;;;;;;;;;;;;;;;;;;;;
  3858.            ;; In tree dired V5.245 Sebastian has used the following
  3859.            ;; trick to resolve symbolic links to directories. This causes
  3860.            ;; havoc with ange-ftp, because ange-ftp expands dots, with
  3861.            ;; expand-file-name before it sends them. This means that this
  3862.            ;; trick currently fails for remote SysV machines. But worse,
  3863.            ;; /vms:/DEV:/FOO/. expands to /vms:/DEV:/FOO, which converts
  3864.            ;; to DEV:FOO and not DEV:[FOO]. i.e it is only in UNIX that
  3865.            ;; we can play fast and loose with the difference between
  3866.            ;; directory names and their associated filenames.
  3867.            ;; My temporary fix is to knock Sebastian's dot off.
  3868.            ;; Maybe things can be made real clever in
  3869.            ;; the future, so that Sebastian can have his way with remote
  3870.            ;; SysV machines.
  3871.            ;; Sebastian in dired-readin-insert says:
  3872.  
  3873.               ;; On SysV derived system, symbolic links to
  3874.           ;; directories are not resolved, while on BSD
  3875.           ;; derived it suffices to let DIRNAME end in slash.
  3876.           ;; We always let it end in "/." since it does no
  3877.           ;; harm on BSD and makes Dired work on such links on
  3878.           ;; SysV.
  3879.  
  3880.         (if (string-match "/\\.$" path)
  3881.         (setq
  3882.          file
  3883.          (ange-ftp-replace-path-component
  3884.           file (substring path 0 -1))))
  3885.         (if (string-match "R" switches)
  3886.         (progn
  3887.           (insert (ange-ftp-ls file switches nil))
  3888.           ;; fix up the subdirectory names in the recursive
  3889.           ;; listing.
  3890.           (ange-ftp-dired-fixup-subdirs pt file))
  3891.           (insert
  3892.            (ange-ftp-ls file
  3893.                 switches
  3894.                 (and (or dumb (string-match "[aA]" switches))
  3895.                  full-directory-p))))
  3896.         (if (and (null full-directory-p)
  3897.              (setq trim-func
  3898.                (cdr (assq host-type
  3899.                       ange-ftp-dired-ls-trim-alist))))
  3900.         ;; If full-directory-p and wild-card are null, then only one
  3901.         ;; line per file must be inserted.
  3902.         ;; Some OS's (like VMS) insert other crap. Clean it out.
  3903.         (save-restriction
  3904.           (narrow-to-region pt (point))
  3905.           (funcall trim-func)))))
  3906.       (ange-ftp-real-dired-ls file switches wildcard full-directory-p))))
  3907.  
  3908. (defvar ange-ftp-remote-shell-file-name
  3909.   (if (memq system-type '(hpux usg-unix-v)) ; hope that's right
  3910.       "remsh"
  3911.     "rsh")
  3912.   "Remote shell used by ange-ftp.")
  3913.  
  3914. (defun ange-ftp-dired-run-shell-command (command &optional in-background)
  3915.   "Documented as original."
  3916.   (let* ((parsed (ange-ftp-ftp-path default-directory))
  3917.      (host (nth 0 parsed))
  3918.      (user (nth 1 parsed))
  3919.      (path (nth 2 parsed)))
  3920.     (if (not parsed)
  3921.     (ange-ftp-real-dired-run-shell-command command in-background)
  3922.       (if (> (length path) 0)        ; else it's $HOME
  3923.       (setq command (concat "cd " path "; " command)))
  3924.       (setq command
  3925.         (format  "%s %s \"%s\""    ; remsh -l USER does not work well
  3926.                     ; on a hp-ux machine I tried
  3927.              ange-ftp-remote-shell-file-name host command))
  3928.       (ange-ftp-message "Remote command '%s' ..." command)
  3929.       ;; Cannot call ange-ftp-real-dired-run-shell-command here as it
  3930.       ;; would prepend "cd default-directory" --- which bombs because
  3931.       ;; default-directory is in ange-ftp syntax for remote path names.
  3932.       (if in-background
  3933.       (comint::background command)
  3934.     (shell-command command)))))
  3935.  
  3936. (defun ange-ftp-make-directory (dir)
  3937.   "Documented as original."
  3938.   (interactive (list (expand-file-name (read-file-name "Make directory: "))))
  3939.   (if (file-exists-p dir)
  3940.       (error "Cannot make directory %s: file already exists" dir)
  3941.     (let ((parsed (ange-ftp-ftp-path dir)))
  3942.       (if parsed
  3943.       (let* ((host (nth 0 parsed))
  3944.          (user (nth 1 parsed))
  3945.          ;; Some ftp's on unix machines (at least on Suns)
  3946.          ;; insist that mkdir take a filename, and not a
  3947.          ;; directory-name name as an arg. Argh!! This is a bug.
  3948.          ;; Non-unix machines will probably always insist
  3949.          ;; that mkdir takes a directory-name as an arg
  3950.          ;; (as the ftp man page says it should).
  3951.          (path (ange-ftp-quote-string
  3952.             (if (eq (ange-ftp-host-type host) 'unix)
  3953.                 (ange-ftp-real-directory-file-name (nth 2 parsed))
  3954.               (ange-ftp-real-file-name-as-directory
  3955.                (nth 2 parsed)))))
  3956.          (abbr (ange-ftp-abbreviate-filename dir))
  3957.          (result (ange-ftp-send-cmd host user
  3958.                         (list 'mkdir path)
  3959.                         (format "Making directory %s"
  3960.                             abbr))))
  3961.         (or (car result)
  3962.         (ange-ftp-error host user
  3963.                 (format "Could not make directory %s: %s"
  3964.                     dir
  3965.                     (cdr result))))
  3966.         (ange-ftp-add-file-entry dir t))
  3967.     (ange-ftp-real-make-directory dir)))))
  3968.  
  3969. (defun ange-ftp-remove-directory (dir)
  3970.   "Documented as original."
  3971.   (interactive 
  3972.    (list (expand-file-name (read-file-name "Remove directory: "
  3973.                        nil nil 'confirm)))) 
  3974.   (if (file-directory-p dir)
  3975.       (let ((parsed (ange-ftp-ftp-path dir)))
  3976.     (if parsed
  3977.         (let* ((host (nth 0 parsed))
  3978.            (user (nth 1 parsed))
  3979.            ;; Some ftp's on unix machines (at least on Suns)
  3980.            ;; insist that rmdir take a filename, and not a
  3981.            ;; directory-name name as an arg. Argh!! This is a bug.
  3982.            ;; Non-unix machines will probably always insist
  3983.            ;; that rmdir takes a directory-name as an arg
  3984.            ;; (as the ftp man page says it should).
  3985.            (path (ange-ftp-quote-string
  3986.               (if (eq (ange-ftp-host-type host) 'unix)
  3987.                   (ange-ftp-real-directory-file-name
  3988.                    (nth 2 parsed))
  3989.                 (ange-ftp-real-file-name-as-directory
  3990.                  (nth 2 parsed)))))
  3991.           (abbr (ange-ftp-abbreviate-filename dir))
  3992.           (result (ange-ftp-send-cmd host user
  3993.                          (list 'rmdir path)
  3994.                          (format "Removing directory %s"
  3995.                              abbr))))
  3996.           (or (car result)
  3997.           (ange-ftp-error host user
  3998.                   (format "Could not remove directory %s: %s"
  3999.                       dir
  4000.                       (cdr result))))
  4001.           (ange-ftp-delete-file-entry dir t))
  4002.       (ange-ftp-real-remove-directory dir)))
  4003.     (error "Not a directory: %s" dir)))
  4004.  
  4005. (defun ange-ftp-diff (fn1 fn2 &optional switches)
  4006.   "Documented as original."
  4007.   (interactive (diff-read-args "Diff: " "Diff %s with: "
  4008.                    "Diff with switches: "))
  4009.   (or (and (stringp fn1)
  4010.        (stringp fn2))
  4011.       (error  "diff: arguments must be strings: %s %s" fn1 fn2))
  4012.   (or switches
  4013.       (setq switches (if (stringp diff-switches)
  4014.              diff-switches
  4015.                (if (listp diff-switches)
  4016.                (mapconcat 'identity diff-switches " ")
  4017.              ""))))
  4018.   (let* ((fn1 (expand-file-name fn1))
  4019.      (fn2 (expand-file-name fn2))
  4020.      (pa1 (ange-ftp-ftp-path fn1))
  4021.      (pa2 (ange-ftp-ftp-path fn2)))
  4022.     (if (or pa1 pa2)
  4023.     (let* ((tmp1 (and pa1 (ange-ftp-make-tmp-name (car pa1))))
  4024.            (tmp2 (and pa2 (ange-ftp-make-tmp-name (car pa2))))
  4025.            (bin1 (and pa1 (ange-ftp-binary-file fn1)))
  4026.            (bin2 (and pa2 (ange-ftp-binary-file fn2)))
  4027.            (dir1 (file-directory-p fn1))
  4028.            (dir2 (file-directory-p fn2))
  4029.            (old-dir default-directory)
  4030.            (default-directory "/tmp")) ;fool FTP-smart compile.el
  4031.       (unwind-protect
  4032.           (progn
  4033.         (if (and dir1 dir2)
  4034.             (error "can't compare remote directories"))
  4035.         (if dir1
  4036.             (setq fn1 (expand-file-name (file-name-nondirectory fn2)
  4037.                         fn1)
  4038.               pa1 (ange-ftp-ftp-path fn1)
  4039.               bin1 (ange-ftp-binary-file fn1)))
  4040.         (if dir2
  4041.             (setq fn2 (expand-file-name (file-name-nondirectory fn1)
  4042.                         fn2)
  4043.               pa2 (ange-ftp-ftp-path fn2)
  4044.               bin2 (ange-ftp-binary-file fn2)))
  4045.         (and pa1 (ange-ftp-copy-file-internal fn1 tmp1 t nil
  4046.                    (format "Getting %s" fn1)))
  4047.         (and pa2 (ange-ftp-copy-file-internal fn2 tmp2 t nil
  4048.                    (format "Getting %s" fn2)))
  4049.         (and ange-ftp-process-verbose
  4050.              (ange-ftp-message "doing diff..."))
  4051.         (sit-for 0)
  4052.         (ange-ftp-real-diff (or tmp1 fn1) (or tmp2 fn2) switches)
  4053.         (cond ((boundp 'compilation-process)
  4054.                (while (and compilation-process
  4055.                    (eq (process-status compilation-process)
  4056.                        'run))
  4057.              (accept-process-output compilation-process)))
  4058.               ((boundp 'compilation-last-buffer)
  4059.                (while (and compilation-last-buffer
  4060.                    (buffer-name compilation-last-buffer)
  4061.                    (get-buffer-process
  4062.                     compilation-last-buffer)
  4063.                    (eq (process-status
  4064.                     (get-buffer-process
  4065.                      compilation-last-buffer))
  4066.                        'run))
  4067.              (accept-process-output))))
  4068.         (and ange-ftp-process-verbose
  4069.              (ange-ftp-message "doing diff...done"))
  4070.         (save-excursion
  4071.           (set-buffer (get-buffer-create "*compilation*"))
  4072.  
  4073.           ;; replace the default directory that we munged earlier.
  4074.           (goto-char (point-min))
  4075.           (if (search-forward (concat "cd " default-directory) nil t)
  4076.               (replace-match (concat "cd " old-dir)))
  4077.           (setq default-directory old-dir)
  4078.  
  4079.           ;; massage the diff output, replacing the temporary file-
  4080.           ;; names with their original names.
  4081.           (if tmp1
  4082.               (let ((q1 (shell-quote tmp1)))
  4083.             (goto-char (point-min))
  4084.             (while (search-forward q1 nil t)
  4085.               (replace-match fn1))))
  4086.           (if tmp2
  4087.               (let ((q2 (shell-quote tmp2)))
  4088.             (goto-char (point-min))
  4089.             (while (search-forward q2 nil t)
  4090.               (replace-match fn2))))))
  4091.         (and tmp1 (ange-ftp-del-tmp-name tmp1))
  4092.         (and tmp2 (ange-ftp-del-tmp-name tmp2))))
  4093.       (ange-ftp-real-diff fn1 fn2 switches))))
  4094.         
  4095. (defun ange-ftp-dired-call-process (program discard &rest arguments)
  4096.   "Documented as original."
  4097.   ;; PROGRAM is always one of those below in the cond in dired.el.
  4098.   ;; The ARGUMENTS are (nearly) always files.
  4099.   (if (ange-ftp-ftp-path default-directory)
  4100.       ;; Can't use ange-ftp-dired-host-type here because the current
  4101.       ;; buffer is *dired-check-process output*
  4102.       (condition-case oops
  4103.       (cond ((equal "compress" program)
  4104.          (ange-ftp-call-compress arguments))
  4105.         ((equal "uncompress" program)
  4106.          (ange-ftp-call-uncompress arguments))
  4107.         ((equal "chmod" program)
  4108.          (ange-ftp-call-chmod arguments))
  4109.         ;; ((equal "chgrp" program))
  4110.         ;; ((equal dired-chown-program program))
  4111.         (t (error "Unknown remote command: %s" program)))
  4112.     (ftp-error (insert (format "%s: %s, %s\n"
  4113.                     (nth 1 oops)
  4114.                     (nth 2 oops)
  4115.                     (nth 3 oops))))
  4116.     (error (insert (format "%s\n" (nth 1 oops)))))
  4117.     (apply 'call-process program nil (not discard) nil arguments)))
  4118.  
  4119.  
  4120. (defun ange-ftp-call-compress (args)
  4121.   "Perform a compress command on a remote file.
  4122. Works by taking a copy of the file, compressing it and copying the file
  4123. back."
  4124.   (if (or (not (= (length args) 2))
  4125.       (not (string-equal "-f" (car args))))
  4126.       (error
  4127.        "ange-ftp-call-compress: missing -f flag and/or missing filename: %s"
  4128.        args))
  4129.   (let* ((file (nth 1 args))
  4130.      (parsed (ange-ftp-ftp-path file))
  4131.      (tmp1 (ange-ftp-make-tmp-name (car parsed)))
  4132.      (tmp2 (ange-ftp-make-tmp-name (car parsed)))
  4133.      (abbr (ange-ftp-abbreviate-filename file))
  4134.      (nfile (ange-ftp-dired-compress-make-compressed-filename file))
  4135.      (nabbr (ange-ftp-abbreviate-filename nfile))
  4136.      (msg1 (format "Getting %s" abbr))
  4137.      (msg2 (format "Putting %s" nabbr)))
  4138.     (unwind-protect
  4139.     (progn
  4140.       (ange-ftp-copy-file-internal file tmp1 t nil msg1)
  4141.       (and ange-ftp-process-verbose
  4142.            (ange-ftp-message "Compressing %s..." abbr))
  4143.       (call-process-region (point)
  4144.                    (point)
  4145.                    shell-file-name
  4146.                    nil
  4147.                    t
  4148.                    nil
  4149.                    "-c"
  4150.                    (format "compress -f -c < %s > %s" tmp1 tmp2))
  4151.       (and ange-ftp-process-verbose
  4152.            (ange-ftp-message "Compressing %s...done" abbr))
  4153.       (if (zerop (buffer-size))
  4154.           (progn
  4155.         (let (ange-ftp-process-verbose)
  4156.           (delete-file file))
  4157.         (ange-ftp-copy-file-internal tmp2 nfile t nil msg2))))
  4158.       (ange-ftp-del-tmp-name tmp1)
  4159.       (ange-ftp-del-tmp-name tmp2))))
  4160.     
  4161. (defun ange-ftp-call-uncompress (args)
  4162.   "Perform an uncompress command on a remote file.
  4163. Works by taking a copy of the file, uncompressing it and copying the file
  4164. back."
  4165.   (if (not (= (length args) 1))
  4166.       (error "ange-ftp-call-uncompress: missing filename: %s" args))
  4167.   (let* ((file (car args))
  4168.      (parsed (ange-ftp-ftp-path file))
  4169.      (tmp1 (ange-ftp-make-tmp-name (car parsed)))
  4170.      (tmp2 (ange-ftp-make-tmp-name (car parsed)))
  4171.      (abbr (ange-ftp-abbreviate-filename file))
  4172.      (nfile (ange-ftp-dired-compress-make-compressed-filename file 'reverse))
  4173.      (nabbr (ange-ftp-abbreviate-filename nfile))
  4174.      (msg1 (format "Getting %s" abbr))
  4175.      (msg2 (format "Putting %s" nabbr))
  4176. ;;     ;; Cheap hack because of problems with binary file transfers from
  4177. ;;     ;; VMS hosts.
  4178. ;;     (gbinary (not (eq 'vms (ange-ftp-host-type (car parsed)))))
  4179.      )
  4180.     (unwind-protect
  4181.     (progn
  4182.       (ange-ftp-copy-file-internal file tmp1 t nil msg1)
  4183.       (and ange-ftp-process-verbose
  4184.            (ange-ftp-message "Uncompressing %s..." abbr))
  4185.       (call-process-region (point)
  4186.                    (point)
  4187.                    shell-file-name
  4188.                    nil
  4189.                    t
  4190.                    nil
  4191.                    "-c"
  4192.                    (format "uncompress -c < %s > %s" tmp1 tmp2))
  4193.       (and ange-ftp-process-verbose
  4194.            (ange-ftp-message "Uncompressing %s...done" abbr))
  4195.       (if (zerop (buffer-size))
  4196.           (progn
  4197.         (let (ange-ftp-process-verbose)
  4198.           (delete-file file))
  4199.         (ange-ftp-copy-file-internal tmp2 nfile t nil msg2))))
  4200.       (ange-ftp-del-tmp-name tmp1)
  4201.       (ange-ftp-del-tmp-name tmp2))))
  4202.  
  4203. (defun ange-ftp-call-chmod (args)
  4204.   (if (< (length args) 2)
  4205.       (error "ange-ftp-call-chmod: missing mode and/or filename: %s" args))
  4206.   (let ((mode (car args)))
  4207.     (mapcar
  4208.      (function
  4209.       (lambda (file)
  4210.     (setq file (expand-file-name file))
  4211.     (let ((parsed (ange-ftp-ftp-path file)))
  4212.       (if parsed
  4213.           (let* ((host (nth 0 parsed))
  4214.              (user (nth 1 parsed))
  4215.              (path (ange-ftp-quote-string (nth 2 parsed)))
  4216.              (abbr (ange-ftp-abbreviate-filename file))
  4217.              (result (ange-ftp-send-cmd host user
  4218.                         (list 'chmod mode path)
  4219.                         (format "doing chmod %s"
  4220.                             abbr))))
  4221.         (or (car result)
  4222.             (ange-ftp-error host user
  4223.                     (format "chmod: %s: \"%s\""
  4224.                         file 
  4225.                         (cdr result)))))))))
  4226.      (cdr args)))
  4227.   (setq ange-ftp-ls-cache-file nil))    ;stop confusing dired
  4228.  
  4229. ;; Need to abstract the way dired computes the names of compressed files.
  4230. ;; I feel badly about these two overloads.
  4231.  
  4232. (defun ange-ftp-dired-compress ()
  4233.   ;; Compress current file.  Return nil for success, offending filename else.
  4234.   (let* (buffer-read-only
  4235.      (from-file (dired-get-filename))
  4236.      (to-file (ange-ftp-dired-compress-make-compressed-filename from-file)))
  4237.     (cond ((save-excursion (beginning-of-line)
  4238.                (looking-at dired-re-sym))
  4239.        (dired-log (concat "Attempt to compress a symbolic link:\n"
  4240.                   from-file))
  4241.        (dired-make-relative from-file))
  4242.       ((dired-check-process (concat "Compressing " from-file)
  4243.                 "compress" "-f" from-file)
  4244.        ;; errors from the process are already logged by
  4245.        ;; dired-check-process
  4246.        (dired-make-relative from-file))
  4247.       (t
  4248.      (dired-update-file-line to-file)
  4249.      nil))))
  4250.  
  4251. (defun ange-ftp-dired-uncompress ()
  4252.   ;; Uncompress current file.  Return nil for success,
  4253.   ;; offending filename else.
  4254.   (let* (buffer-read-only
  4255.      (from-file (dired-get-filename))
  4256.      (to-file (ange-ftp-dired-compress-make-compressed-filename from-file 'reverse)))
  4257.     (if (dired-check-process (concat "Uncompressing " from-file)
  4258.                  "uncompress" from-file)
  4259.     (dired-make-relative from-file)
  4260.       (dired-update-file-line to-file)
  4261.       nil)))
  4262.  
  4263. (defun ange-ftp-dired-flag-backup-files (&optional unflag-p)
  4264.   "Documented as original."
  4265.   (interactive "P")
  4266.   (funcall (or (and ange-ftp-dired-host-type
  4267.             (cdr (assq ange-ftp-dired-host-type
  4268.                    ange-ftp-dired-flag-backup-files-alist)))
  4269.            'ange-ftp-real-dired-flag-backup-files)
  4270.        unflag-p))
  4271.  
  4272. ;;; ------------------------------------------------------------
  4273. ;;; Noddy support for async copy-file within dired.
  4274. ;;; ------------------------------------------------------------
  4275.  
  4276. (defun ange-ftp-dired-copy-file (from to ok-flag &optional cont nowait)
  4277.   "Documented as original."
  4278.   (dired-handle-overwrite to)
  4279.   (ange-ftp-copy-file-internal from to ok-flag dired-copy-preserve-time nil
  4280.                    cont nowait))
  4281.  
  4282. (defun ange-ftp-dired-do-create-files (op-symbol file-creator operation arg
  4283.                          &optional marker-char op1
  4284.                          how-to)
  4285.   "Documented as original."
  4286.   ;; we need to let ange-ftp-dired-create-files know that we indirectly
  4287.   ;; called it rather than somebody else.
  4288.   (let ((ange-ftp-dired-do-create-files t)) ; tell who caller is
  4289.     (ange-ftp-real-dired-do-create-files op-symbol file-creator operation
  4290.                      arg marker-char op1 how-to)))
  4291.  
  4292. (defun ange-ftp-dired-create-files (file-creator operation fn-list name-constructor
  4293.                          &optional marker-char)
  4294.   "Documented as original."
  4295.   (if (and (boundp 'ange-ftp-dired-do-create-files)
  4296.        ;; called from ange-ftp-dired-do-create-files?
  4297.        ange-ftp-dired-do-create-files
  4298.        ;; any files worth copying?
  4299.        fn-list
  4300.        ;; we only support async copy-file at the mo.
  4301.        (eq file-creator 'dired-copy-file)
  4302.        ;; it is only worth calling the alternative function for remote files
  4303.        ;; as we tie ourself in recursive knots otherwise.
  4304.        (or (ange-ftp-ftp-path (car fn-list))
  4305.            ;; we can only call the name constructor for dired-do-create-files
  4306.            ;; since the one for regexps starts prompting here, there and
  4307.            ;; everywhere.
  4308.            (ange-ftp-ftp-path (funcall name-constructor (car fn-list)))))
  4309.       ;; use the process-filter driven routine rather than the iterative one.
  4310.       (ange-ftp-dcf-1 file-creator
  4311.                  operation
  4312.                fn-list
  4313.                name-constructor
  4314.                (and (boundp 'target) target)    ;dynamically bound
  4315.                marker-char
  4316.                (current-buffer)
  4317.                nil    ;overwrite-query
  4318.                nil    ;overwrite-backup-query
  4319.                nil    ;failures
  4320.                nil    ;skipped
  4321.                0        ;success-count
  4322.                (length fn-list) ;total
  4323.                )
  4324.     ;; normal case... use the interative routine... much cheaper.
  4325.     (ange-ftp-real-dired-create-files file-creator operation fn-list
  4326.                       name-constructor marker-char)))
  4327.  
  4328. (defun ange-ftp-dcf-1 (file-creator operation fn-list name-constructor
  4329.                target marker-char buffer overwrite-query 
  4330.                overwrite-backup-query failures skipped
  4331.                success-count total)
  4332.   (let ((old-buf (current-buffer)))
  4333.     (unwind-protect
  4334.     (progn
  4335.       (set-buffer buffer)
  4336.       (if (null fn-list)
  4337.           (ange-ftp-dcf-3 failures operation total skipped
  4338.                   success-count buffer)
  4339.         
  4340.         (let* ((from (car fn-list))
  4341.            (to (funcall name-constructor from)))
  4342.           (if (equal to from)
  4343.           (progn
  4344.             (setq to nil)
  4345.             (dired-log "Cannot %s to same file: %s\n"
  4346.                    (downcase operation) from)))
  4347.           (if (not to)
  4348.           (ange-ftp-dcf-1 file-creator
  4349.                   operation
  4350.                   (cdr fn-list)
  4351.                   name-constructor
  4352.                   target
  4353.                   marker-char
  4354.                   buffer
  4355.                   overwrite-query
  4356.                   overwrite-backup-query
  4357.                   failures
  4358.                   (cons (dired-make-relative from) skipped)
  4359.                   success-count
  4360.                   total)
  4361.         (let* ((overwrite (file-exists-p to))
  4362.                (overwrite-confirmed    ; for dired-handle-overwrite
  4363.             (and overwrite
  4364.                  (let ((help-form '(format "\
  4365. Type SPC or `y' to overwrite file `%s',
  4366. DEL or `n' to skip to next,
  4367. ESC or `q' to not overwrite any of the remaining files,
  4368. `!' to overwrite all remaining files with no more questions." to)))
  4369.                    (dired-query 'overwrite-query
  4370.                         "Overwrite `%s'?" to))))
  4371.                ;; must determine if FROM is marked before file-creator
  4372.                ;; gets a chance to delete it (in case of a move).
  4373.                (actual-marker-char
  4374.             (cond  ((integerp marker-char) marker-char)
  4375.                    (marker-char (dired-file-marker from)) ; slow
  4376.                    (t nil))))
  4377.           (condition-case err
  4378.               (funcall file-creator from to overwrite-confirmed
  4379.                    (list (function ange-ftp-dcf-2)
  4380.                      nil    ;err
  4381.                      file-creator operation fn-list
  4382.                      name-constructor
  4383.                      target
  4384.                      marker-char actual-marker-char
  4385.                      buffer to from
  4386.                      overwrite
  4387.                      overwrite-confirmed
  4388.                      overwrite-query 
  4389.                      overwrite-backup-query
  4390.                      failures skipped success-count
  4391.                      total)
  4392.                    t)
  4393.             (file-error        ; FILE-CREATOR aborted
  4394.              (ange-ftp-dcf-2 nil ;result
  4395.                      nil ;line
  4396.                      err
  4397.                      file-creator operation fn-list
  4398.                      name-constructor
  4399.                      target
  4400.                      marker-char actual-marker-char
  4401.                      buffer to from
  4402.                      overwrite
  4403.                      overwrite-confirmed
  4404.                      overwrite-query 
  4405.                      overwrite-backup-query
  4406.                      failures skipped success-count
  4407.                      total))))))))
  4408.       (set-buffer old-buf))))
  4409.  
  4410. (defun ange-ftp-dcf-2 (result line err
  4411.                   file-creator operation fn-list
  4412.                   name-constructor
  4413.                   target
  4414.                   marker-char actual-marker-char
  4415.                   buffer to from
  4416.                   overwrite
  4417.                   overwrite-confirmed
  4418.                   overwrite-query 
  4419.                   overwrite-backup-query
  4420.                   failures skipped success-count
  4421.                   total)
  4422.   (let ((old-buf (current-buffer)))
  4423.     (unwind-protect
  4424.     (progn
  4425.       (set-buffer buffer)
  4426.       (if (or err (not result))
  4427.           (progn
  4428.         (setq failures (cons (dired-make-relative from) failures))
  4429.         (dired-log "%s `%s' to `%s' failed:\n%s\n"
  4430.                operation from to (or err line)))
  4431.         (if overwrite
  4432.         ;; If we get here, file-creator hasn't been aborted
  4433.         ;; and the old entry (if any) has to be deleted
  4434.         ;; before adding the new entry.
  4435.         (dired-remove-file to))
  4436.         (setq success-count (1+ success-count))
  4437.         (message "%s: %d of %d" operation success-count total)
  4438.         (dired-add-file to actual-marker-char))
  4439.       
  4440.       (ange-ftp-dcf-1 file-creator operation (cdr fn-list)
  4441.               name-constructor
  4442.               target
  4443.               marker-char
  4444.               buffer
  4445.               overwrite-query 
  4446.               overwrite-backup-query
  4447.               failures skipped success-count
  4448.               total))
  4449.       (set-buffer old-buf))))
  4450.  
  4451. (defun ange-ftp-dcf-3 (failures operation total skipped success-count
  4452.                 buffer)
  4453.   (let ((old-buf (current-buffer)))
  4454.     (unwind-protect
  4455.     (progn
  4456.       (set-buffer buffer)
  4457.       (cond
  4458.        (failures
  4459.         (dired-log-summary
  4460.          (message "%s failed for %d of %d file%s %s"
  4461.               operation (length failures) total
  4462.               (dired-plural-s total) failures)))
  4463.        (skipped
  4464.         (dired-log-summary
  4465.          (message "%s: %d of %d file%s skipped %s"
  4466.               operation (length skipped) total
  4467.               (dired-plural-s total) skipped)))
  4468.        (t
  4469.         (message "%s: %s file%s."
  4470.              operation success-count (dired-plural-s success-count))))
  4471.       (dired-move-to-filename))
  4472.       (set-buffer old-buf))))
  4473.  
  4474. ;;;; -----------------------------------------------
  4475. ;;;; Unix Descriptive Listing (dl) Support
  4476. ;;;; -----------------------------------------------
  4477.  
  4478. (defconst ange-ftp-dired-dl-re-dir
  4479.   "^. [^ /]+/[ \n]"
  4480.   "Regular expression to use to search for dl directories.")
  4481.  
  4482. (or (assq 'unix:dl ange-ftp-dired-re-dir-alist)
  4483.     (setq ange-ftp-dired-re-dir-alist
  4484.       (cons (cons 'unix:dl  ange-ftp-dired-dl-re-dir)
  4485.         ange-ftp-dired-re-dir-alist)))
  4486.  
  4487. (defun ange-ftp-dired-dl-move-to-filename (&optional raise-error eol)
  4488.   "In dired, move to the first character of the filename on this line."
  4489.   ;; This is the Unix dl version.
  4490.   (or eol (setq eol (progn (end-of-line) (point))))
  4491.   (let (case-fold-search)
  4492.     (beginning-of-line)
  4493.     (if (looking-at ". [^ ]+ +\\([0-9]+\\|-\\|=\\) ")
  4494.     (goto-char (+ (point) 2))
  4495.       (if raise-error
  4496.       (error "No file on this line")
  4497.     nil))))
  4498.  
  4499. (or (assq 'unix:dl ange-ftp-dired-move-to-filename-alist)
  4500.     (setq ange-ftp-dired-move-to-filename-alist
  4501.       (cons '(unix:dl . ange-ftp-dired-dl-move-to-filename)
  4502.         ange-ftp-dired-move-to-filename-alist)))
  4503.  
  4504. (defun ange-ftp-dired-dl-move-to-end-of-filename (&optional no-error eol)
  4505.   ;; Assumes point is at beginning of filename.
  4506.   ;; So, it should be called only after (dired-move-to-filename t).
  4507.   ;; On failure, signals an error or returns nil.
  4508.   ;; This is the Unix dl version.
  4509.   (let ((opoint (point))
  4510.     case-fold-search hidden)
  4511.     (or eol (setq eol (save-excursion (end-of-line) (point))))
  4512.     (setq hidden (and selective-display
  4513.                (save-excursion
  4514.              (search-forward "\r" eol t))))
  4515.     (if hidden
  4516.     (if no-error
  4517.         nil
  4518.       (error
  4519.        (substitute-command-keys
  4520.         "File line is hidden, type \\[dired-hide-subdir] to unhide")))
  4521.       (skip-chars-forward "^ /" eol)
  4522.       (if (eq opoint (point))
  4523.       (if no-error
  4524.           nil
  4525.         (error "No file on this line"))
  4526.     (point)))))
  4527.  
  4528. (or (assq 'unix:dl ange-ftp-dired-move-to-end-of-filename-alist)
  4529.     (setq ange-ftp-dired-move-to-end-of-filename-alist
  4530.       (cons '(unix:dl . ange-ftp-dired-dl-move-to-end-of-filename)
  4531.         ange-ftp-dired-move-to-end-of-filename-alist)))
  4532.  
  4533. ;;;; ------------------------------------------------------------
  4534. ;;;; File name completion support.
  4535. ;;;; ------------------------------------------------------------
  4536.  
  4537. (defun ange-ftp-file-entry-active-p (sym)
  4538.   "If the file entry is a symlink, returns whether the file pointed to exists.
  4539. Note that DIR is dynamically bound."
  4540.   (let ((val (get sym 'val)))
  4541.     (or (not (stringp val))
  4542.     (file-exists-p (ange-ftp-expand-symlink val dir)))))
  4543.  
  4544. (defun ange-ftp-file-entry-not-ignored-p (sym)
  4545.   "If the file entry is not a directory (nor a symlink pointing to a directory)
  4546. returns whether the file (or file pointed to by the symlink) is ignored
  4547. by completion-ignored-extensions.
  4548. Note that DIR and COMPLETION-IGNORED-PATTERN are dynamically bound."
  4549.   (let ((val (get sym 'val))
  4550.     (symname (symbol-name sym)))
  4551.     (if (stringp val)
  4552.     (let ((file (ange-ftp-expand-symlink val dir)))
  4553.       (or (file-directory-p file)
  4554.           (and (file-exists-p file)
  4555.            (not (string-match completion-ignored-pattern
  4556.                       symname)))))
  4557.       (or val ; is a directory name
  4558.       (not (string-match completion-ignored-pattern symname))))))
  4559.  
  4560. (defun ange-ftp-file-name-all-completions (file dir)
  4561.   "Documented as original."
  4562.   (setq dir (expand-file-name dir))
  4563.   (if (ange-ftp-ftp-path dir)
  4564.       (progn
  4565.     (ange-ftp-barf-if-not-directory dir)
  4566.     (setq dir (ange-ftp-real-file-name-as-directory dir))
  4567.     (let* ((tbl (ange-ftp-get-files dir))
  4568.            (completions
  4569.         (all-completions file tbl
  4570.                  (function ange-ftp-file-entry-active-p))))
  4571.       
  4572.       ;; see whether each matching file is a directory or not...
  4573.       (mapcar
  4574.        (function
  4575.         (lambda (file)
  4576.           (let ((ent (ange-ftp-get-hash-entry file tbl)))
  4577.         (if (and ent
  4578.              (or (not (stringp ent))
  4579.                  (file-directory-p
  4580.                   (ange-ftp-expand-symlink ent dir))))
  4581.             (concat file "/")
  4582.           file))))
  4583.        completions)))
  4584.     
  4585.     (if (string-equal "/" dir)
  4586.     (nconc (all-completions file (ange-ftp-generate-root-prefixes))
  4587.            (ange-ftp-real-file-name-all-completions file dir))
  4588.       (ange-ftp-real-file-name-all-completions file dir))))
  4589.  
  4590. (defun ange-ftp-file-name-completion (file dir)
  4591.   "Documented as original."
  4592.   (setq dir (expand-file-name dir))
  4593.   (if (ange-ftp-ftp-path dir)
  4594.       (progn
  4595.     (ange-ftp-barf-if-not-directory dir)
  4596.     (if (equal file "")
  4597.         ""
  4598.       (setq dir (ange-ftp-real-file-name-as-directory dir))    ;real?
  4599.       (let* ((tbl (ange-ftp-get-files dir))
  4600.          (completion-ignored-pattern
  4601.           (mapconcat (function
  4602.                   (lambda (s) (if (stringp s)
  4603.                           (concat (regexp-quote s) "$")
  4604.                         "/"))) ; / never in filename
  4605.                  completion-ignored-extensions
  4606.                  "\\|")))
  4607.         (ange-ftp-save-match-data
  4608.           (or (ange-ftp-file-name-completion-1
  4609.            file tbl dir (function ange-ftp-file-entry-not-ignored-p))
  4610.           (ange-ftp-file-name-completion-1
  4611.            file tbl dir (function ange-ftp-file-entry-active-p)))))))
  4612.     (if (string-equal "/" dir)
  4613.     (try-completion
  4614.      file
  4615.      (nconc (ange-ftp-generate-root-prefixes)
  4616.         (mapcar 'list
  4617.             (ange-ftp-real-file-name-all-completions file "/"))))
  4618.       (ange-ftp-real-file-name-completion file dir))))
  4619.  
  4620.  
  4621. (defun ange-ftp-file-name-completion-1 (file tbl dir predicate)
  4622.   "Internal subroutine for ange-ftp-file-name-completion.  Do not call this."
  4623.   (let ((bestmatch (try-completion file tbl predicate)))
  4624.     (if bestmatch
  4625.     (if (eq bestmatch t)
  4626.         (if (file-directory-p (expand-file-name file dir))
  4627.         (concat file "/")
  4628.           t)
  4629.       (if (and (eq (try-completion bestmatch tbl predicate) t)
  4630.            (file-directory-p
  4631.             (expand-file-name bestmatch dir)))
  4632.           (concat bestmatch "/")
  4633.         bestmatch)))))
  4634.  
  4635. (defun ange-ftp-quote-filename (file)
  4636.   "Quote `$' as `$$' in FILE to get it past function `substitute-in-file-name.'"
  4637.   (let ((pos 0))
  4638.     (while (setq pos (string-match "\\$" file pos))
  4639.       (setq file (concat (substring file 0 pos)
  4640.              "$";; precede by escape character (also a $)
  4641.              (substring file pos))
  4642.         ;; add 2 instead 1 since another $ was inserted
  4643.         pos (+ 2 pos)))
  4644.     file))
  4645.  
  4646. (defun ange-ftp-read-file-name-internal (string dir action)
  4647.   "Documented as original."
  4648.   (let (name realdir)
  4649.     (if (eq action 'lambda)
  4650.     (if (> (length string) 0)
  4651.         (file-exists-p (substitute-in-file-name string)))
  4652.       (if (zerop (length string))
  4653.      (setq name string realdir dir)
  4654.        (setq string (substitute-in-file-name string)
  4655.           name (file-name-nondirectory string)
  4656.           realdir (file-name-directory string))
  4657.     (setq realdir (if realdir (expand-file-name realdir dir) dir)))
  4658.      (if action
  4659.       (file-name-all-completions name realdir)
  4660.     (let ((specdir (file-name-directory string))
  4661.           (val (file-name-completion name realdir)))
  4662.       (if (and specdir (stringp val))
  4663.           (ange-ftp-quote-filename (concat specdir val))
  4664.         val))))))
  4665.  
  4666. ;; Put these lines uncommmented in your .emacs if you want C-r to refresh
  4667. ;; ange-ftp's cache whilst doing filename completion.
  4668. ;;
  4669. ;;(define-key minibuffer-local-completion-map "\C-r" 'ange-ftp-re-read-dir)
  4670. ;;(define-key minibuffer-local-must-match-map "\C-r" 'ange-ftp-re-read-dir)
  4671.  
  4672. (defun ange-ftp-re-read-dir (&optional dir)
  4673.   "Forces a re-read of the directory DIR.  If DIR is omitted then it defaults
  4674. to the directory part of the contents of the current buffer."
  4675.   (interactive)
  4676.   (if dir
  4677.       (setq dir (expand-file-name dir))
  4678.     (setq dir (file-name-directory (expand-file-name (buffer-string)))))
  4679.   (if (ange-ftp-ftp-path dir)
  4680.       (progn
  4681.     (setq ange-ftp-ls-cache-file nil)
  4682.     (ange-ftp-del-hash-entry dir ange-ftp-files-hashtable)
  4683.     (ange-ftp-get-files dir t))))
  4684.  
  4685. ;;;; ------------------------------------------------------------
  4686. ;;;; Bits and bobs to bolt ange-ftp into GNU Emacs.
  4687. ;;;; ------------------------------------------------------------
  4688.  
  4689. (defvar ange-ftp-overwrite-msg
  4690.   "Note: This function has been modified to work with ange-ftp.")
  4691.  
  4692. (defun ange-ftp-safe-documentation (fun)
  4693.   "A documentation function that isn't quite as fragile."
  4694.   (condition-case ()
  4695.       (documentation fun)
  4696.     (error nil)))
  4697.  
  4698. (defun ange-ftp-overwrite-fn (fun)
  4699.   "Replace FUN's function definition with ange-ftp-FUN's, saving the
  4700. original definition as ange-ftp-real-FUN.  The original documentation is
  4701. placed on the new definition suitably augmented."
  4702.   (let* ((name (symbol-name fun))
  4703.      (saved (intern (concat "ange-ftp-real-" name)))
  4704.      (new (intern (concat "ange-ftp-" name)))
  4705.      (nfun (symbol-function new))
  4706.      (exec-directory (if (or (equal (nth 3 command-line-args) "dump")
  4707.                  (equal (nth 4 command-line-args) "dump"))
  4708.                  "../etc/"
  4709.                exec-directory)))             
  4710.     
  4711.     ;; *** This is unnecessary for any ange-ftp function (I think):
  4712.     (while (symbolp nfun)
  4713.       (setq nfun (symbol-function nfun)))
  4714.     
  4715.     ;; Interpose the ange-ftp function between the function symbol and the
  4716.     ;; original definition of the function symbol AT TIME OF FIRST LOAD.
  4717.     ;; We must only redefine the symbol-function of FUN the very first
  4718.     ;; time, to avoid blowing away stuff that overloads FUN after this.
  4719.     
  4720.     ;; We direct the function symbol to the ange-ftp's function symbol
  4721.     ;; rather than function definition to allow reloading of this file or
  4722.     ;; redefining of the individual function (e.g., during debugging)
  4723.     ;; later after some other code has been loaded on top of our stuff.
  4724.     
  4725.     (or (fboundp saved)
  4726.     (progn
  4727.       (fset saved (symbol-function fun))
  4728.       (fset fun new)))
  4729.     
  4730.     ;; Rewrite the doc string on the new ange-ftp function.  This should
  4731.     ;; be done every time the file is loaded (or a function is redefined),
  4732.     ;; because the underlying overloaded function may have changed its doc
  4733.     ;; string.
  4734.     
  4735.     (let* ((doc-str (ange-ftp-safe-documentation saved))
  4736.        (ndoc-str (concat doc-str (and doc-str "\n")
  4737.                  ange-ftp-overwrite-msg)))
  4738.       
  4739.       (cond ((listp nfun)
  4740.          ;; Probe to test whether function is in preloaded read-only
  4741.          ;; memory, and if so make writable copy:
  4742.          (condition-case nil
  4743.          (setcar nfun (car nfun))
  4744.            (error
  4745.         (setq nfun (copy-sequence nfun)) ; shallow copy only
  4746.         (fset new nfun)))
  4747.          (let ((ndoc-cdr (nthcdr 2 nfun)))
  4748.            (if (stringp (car ndoc-cdr))
  4749.            ;; Replace the existing docstring.
  4750.            (setcar ndoc-cdr ndoc-str)
  4751.          ;; There is no docstring.  Insert the overwrite msg.
  4752.          (setcdr ndoc-cdr (cons (car ndoc-cdr) (cdr ndoc-cdr)))
  4753.          (setcar ndoc-cdr ange-ftp-overwrite-msg))))
  4754.         (t
  4755.          ;; it's an emacs19 compiled-code object
  4756.          (let ((new-code (append nfun nil))) ; turn it into a list
  4757.            (if (nthcdr 4 new-code)
  4758.            (setcar (nthcdr 4 new-code) ndoc-str)
  4759.          (setcdr (nthcdr 3 new-code) (cons ndoc-str nil)))
  4760.            (fset new (apply 'make-byte-code new-code))))))))
  4761.  
  4762. (defun ange-ftp-overwrite-dired ()
  4763.   (if (not (fboundp 'dired-ls))        ;dired should have been loaded by now
  4764.       (ange-ftp-overwrite-fn 'dired-readin) ; classic dired
  4765.     (ange-ftp-overwrite-fn 'make-directory) ; tree dired and v19 stuff
  4766.     (ange-ftp-overwrite-fn 'remove-directory)
  4767.     (ange-ftp-overwrite-fn 'diff)
  4768.     (ange-ftp-overwrite-fn 'dired-run-shell-command)
  4769.     (ange-ftp-overwrite-fn 'dired-ls)
  4770.     (ange-ftp-overwrite-fn 'dired-call-process)
  4771.     ;; Can't use (fset 'ange-ftp-dired-readin 'ange-ftp-tree-dired-readin)
  4772.     ;; here because it confuses ange-ftp-overwrite-fn.
  4773.     (fset 'ange-ftp-dired-readin (symbol-function 'ange-ftp-tree-dired-readin))
  4774.     (ange-ftp-overwrite-fn 'dired-readin)
  4775.     (ange-ftp-overwrite-fn 'dired-insert-headerline)
  4776.     (ange-ftp-overwrite-fn 'dired-move-to-filename)
  4777.     (ange-ftp-overwrite-fn 'dired-move-to-end-of-filename)
  4778.     (ange-ftp-overwrite-fn 'dired-get-filename)
  4779.     (ange-ftp-overwrite-fn 'dired-between-files)
  4780.     (ange-ftp-overwrite-fn 'dired-clean-directory)
  4781.     (ange-ftp-overwrite-fn 'dired-flag-backup-files)
  4782.     (ange-ftp-overwrite-fn 'dired-backup-diff)
  4783.     (if (fboundp 'dired-do-create-files)
  4784.     ;; dired 6.0 or later.
  4785.     (progn
  4786.       (ange-ftp-overwrite-fn 'dired-copy-file)
  4787.       (ange-ftp-overwrite-fn 'dired-create-files)
  4788.       (ange-ftp-overwrite-fn 'dired-do-create-files)))
  4789.     (if (fboundp 'dired-compress-make-compressed-filename)
  4790.     ;; it's V5.255 or later
  4791.     (ange-ftp-overwrite-fn 'dired-compress-make-compressed-filename)
  4792.       ;; ange-ftp-overwrite-fn confuses dired-mark-map here.
  4793.       (fset 'ange-ftp-real-dired-compress (symbol-function 'dired-compress))
  4794.       (fset 'dired-compress 'ange-ftp-dired-compress)
  4795.       (fset 'ange-ftp-real-dired-uncompress (symbol-function 'dired-uncompress))
  4796.       (fset 'dired-uncompress 'ange-ftp-dired-uncompress)))
  4797.  
  4798.   (ange-ftp-overwrite-fn 'dired-find-file)
  4799.   (ange-ftp-overwrite-fn 'dired-revert))
  4800.  
  4801. ;; Attention!
  4802. ;; It would be nice if ange-ftp-add-hook was generalized to
  4803. ;; (defun ange-ftp-add-hook (hook-var hook-function &optional postpend),
  4804. ;; where the optional postpend variable stipulates that hook-function
  4805. ;; should be post-pended to the hook-var, rather than prepended.
  4806. ;; Then, maybe we should overwrite dired with
  4807. ;; (ange-ftp-add-hook 'dired-load-hook 'ange-ftp-overwrite-dired t).
  4808. ;; This is because dired-load-hook is commonly used to add the dired extras
  4809. ;; features (dired-x.el, dired-trns.el, dired-nstd.el, ...). Some of these
  4810. ;; extras features overwrite functions in dired.el with fancier versions.
  4811. ;; The "extras" overwrites would then clobber the ange-ftp overwrites.
  4812. ;; As long as the ange-ftp overwrites are carefully written to use
  4813. ;; ange-ftp-real-... when the directory is local, then doing the ange-ftp
  4814. ;; overwrites after the extras overwites should be OK.
  4815. ;; At the moment, I think that there aren't any conflicts between the extras
  4816. ;; overwrites, and the ange-ftp overwrites. This may not last though.
  4817.  
  4818. (defun ange-ftp-add-hook (hook-var hook-function)
  4819.   "Prepend hook-function to hook-var's value, if it is not already an element.
  4820. hook-var's value may be a single function or a list of functions."
  4821.   (if (boundp hook-var)
  4822.       (let ((value (symbol-value hook-var)))
  4823.         (if (and (listp value) (not (eq (car value) 'lambda)))
  4824.             (and (not (memq hook-function value))
  4825.                  (set hook-var
  4826.                       (if value (cons hook-function value) hook-function)))
  4827.           (and (not (eq hook-function value))
  4828.                (set hook-var
  4829.                     (list hook-function value)))))
  4830.     (set hook-var hook-function)))
  4831.  
  4832. ;; To load ange-ftp and not dired (leaving it to autoload), define
  4833. ;; dired-load-hook and make sure dired.el ends with:
  4834. ;;    (run-hooks 'dired-load-hook)
  4835. ;;
  4836. (if (and (boundp 'dired-load-hook)
  4837.      (not (featurep 'dired)))
  4838.     (ange-ftp-add-hook 'dired-load-hook 'ange-ftp-overwrite-dired)
  4839.   (require 'dired)
  4840.   (ange-ftp-overwrite-dired))
  4841.  
  4842. ;; In case v19 or emacs-19.el already loaded:
  4843. ;; (Can't use fboundp to check if emacs-19.el is
  4844. ;; loaded, because these functions are probably
  4845. ;; bound to autoloads.)
  4846.  
  4847. (if (and (fboundp 'make-directory)
  4848.      (not (and (listp (symbol-function 'make-directory))
  4849.            (eq (car (symbol-function 'make-directory)) 'autoload))))
  4850.     (ange-ftp-overwrite-fn 'make-directory))
  4851. (if (and (fboundp 'remove-directory)
  4852.      (not (and (listp (symbol-function 'remove-directory))
  4853.            (eq (car (symbol-function 'remove-directory)) 'autoload))))
  4854.     (ange-ftp-overwrite-fn 'remove-directory))
  4855. (if (and (fboundp 'diff)
  4856.      (not (and (listp (symbol-function 'diff))
  4857.            (eq (car (symbol-function 'diff)) 'autoload))))
  4858.       (ange-ftp-overwrite-fn 'diff))
  4859.  
  4860. (ange-ftp-overwrite-fn 'insert-file-contents)
  4861. (ange-ftp-overwrite-fn 'directory-files)
  4862. (ange-ftp-overwrite-fn 'file-directory-p)
  4863. (ange-ftp-overwrite-fn 'file-writable-p)
  4864. (ange-ftp-overwrite-fn 'file-readable-p)
  4865. (ange-ftp-overwrite-fn 'file-symlink-p)
  4866. (ange-ftp-overwrite-fn 'delete-file)
  4867. (ange-ftp-overwrite-fn 'read-file-name-internal)
  4868. (ange-ftp-overwrite-fn 'verify-visited-file-modtime)
  4869. (ange-ftp-overwrite-fn 'file-exists-p)
  4870. (ange-ftp-overwrite-fn 'write-region)
  4871. (ange-ftp-overwrite-fn 'backup-buffer)
  4872. (ange-ftp-overwrite-fn 'copy-file)
  4873. (ange-ftp-overwrite-fn 'rename-file)
  4874. (ange-ftp-overwrite-fn 'file-attributes)
  4875. (ange-ftp-overwrite-fn 'file-name-directory)
  4876. (ange-ftp-overwrite-fn 'file-name-nondirectory)
  4877. (ange-ftp-overwrite-fn 'file-name-as-directory)
  4878. (ange-ftp-overwrite-fn 'directory-file-name)
  4879. (ange-ftp-overwrite-fn 'expand-file-name)
  4880. (ange-ftp-overwrite-fn 'file-name-all-completions)
  4881. (ange-ftp-overwrite-fn 'file-name-completion)
  4882.  
  4883. (or (memq 'ange-ftp-set-buffer-mode find-file-hooks)
  4884.     (setq find-file-hooks
  4885.       (cons 'ange-ftp-set-buffer-mode find-file-hooks)))
  4886.  
  4887.  
  4888. ;;;; ------------------------------------------------------------
  4889. ;;;; VOS support (VOS support is probably broken,
  4890. ;;;; but I don't know anything about VOS.)
  4891. ;;;; ------------------------------------------------------------
  4892. ;
  4893. ;(defun ange-ftp-fix-path-for-vos (path &optional reverse)
  4894. ;  (setq path (copy-sequence path))
  4895. ;  (let ((from (if reverse ?\> ?\/))
  4896. ;    (to (if reverse ?\/ ?\>))
  4897. ;    (i (1- (length path))))
  4898. ;    (while (>= i 0)
  4899. ;      (if (= (aref path i) from)
  4900. ;      (aset path i to))
  4901. ;      (setq i (1- i)))
  4902. ;    path))
  4903. ;
  4904. ;(or (assq 'vos ange-ftp-fix-path-func-alist)
  4905. ;    (setq ange-ftp-fix-path-func-alist
  4906. ;      (cons '(vos . ange-ftp-fix-path-for-vos)
  4907. ;        ange-ftp-fix-path-func-alist)))
  4908. ;
  4909. ;(or (memq 'vos ange-ftp-dumb-host-types)
  4910. ;    (setq ange-ftp-dumb-host-types
  4911. ;      (cons 'vos ange-ftp-dumb-host-types)))
  4912. ;
  4913. ;(defun ange-ftp-fix-dir-path-for-vos (dir-path)
  4914. ;  (ange-ftp-fix-path-for-vos
  4915. ;   (concat dir-path
  4916. ;       (if (eq ?/ (aref dir-path (1- (length dir-path))))
  4917. ;           "" "/")
  4918. ;       "*")))
  4919. ;
  4920. ;(or (assq 'vos ange-ftp-fix-dir-path-func-alist)
  4921. ;    (setq ange-ftp-fix-dir-path-func-alist
  4922. ;      (cons '(vos . ange-ftp-fix-dir-path-for-vos)
  4923. ;        ange-ftp-fix-dir-path-func-alist)))
  4924. ;
  4925. ;(defvar ange-ftp-vos-host-regexp nil
  4926. ;  "If a host matches this regexp then it is assumed to be running VOS.")
  4927. ;
  4928. ;(defun ange-ftp-vos-host (host)
  4929. ;  (and ange-ftp-vos-host-regexp
  4930. ;       (ange-ftp-save-match-data
  4931. ;     (string-match ange-ftp-vos-host-regexp host))))
  4932. ;
  4933. ;(defun ange-ftp-parse-vos-listing ()
  4934. ;  "Parse the current buffer which is assumed to be in VOS list -all
  4935. ;format, and return a hashtable as the result."
  4936. ;  (let ((tbl (ange-ftp-make-hashtable))
  4937. ;    (type-list
  4938. ;     '(("^Files: [0-9]+ +Blocks: [0-9]+\n+" nil 40)
  4939. ;       ("^Dirs: [0-9]+\n+" t 30)))
  4940. ;    type-regexp type-is-dir type-col file)
  4941. ;    (goto-char (point-min))
  4942. ;    (ange-ftp-save-match-data
  4943. ;      (while type-list
  4944. ;    (setq type-regexp (car (car type-list))
  4945. ;          type-is-dir (nth 1 (car type-list))
  4946. ;          type-col (nth 2 (car type-list))
  4947. ;          type-list (cdr type-list))
  4948. ;    (if (re-search-forward type-regexp nil t)
  4949. ;        (while (eq (char-after (point)) ? )
  4950. ;          (move-to-column type-col)
  4951. ;          (setq file (buffer-substring (point)
  4952. ;                       (progn
  4953. ;                         (end-of-line 1)
  4954. ;                         (point))))
  4955. ;          (ange-ftp-put-hash-entry file type-is-dir tbl)
  4956. ;          (forward-line 1))))
  4957. ;      (ange-ftp-put-hash-entry "." 'vosdir tbl)
  4958. ;      (ange-ftp-put-hash-entry ".." 'vosdir tbl))
  4959. ;    tbl))
  4960. ;
  4961. ;(or (assq 'vos ange-ftp-parse-list-func-alist)
  4962. ;    (setq ange-ftp-parse-list-func-alist
  4963. ;      (cons '(vos . ange-ftp-parse-vos-listing)
  4964. ;        ange-ftp-parse-list-func-alist)))
  4965.  
  4966. ;;;; ------------------------------------------------------------
  4967. ;;;; VMS support.
  4968. ;;;; ------------------------------------------------------------
  4969.  
  4970. (defun ange-ftp-fix-path-for-vms (path &optional reverse)
  4971.   "Convert PATH from UNIX-ish to VMS.  If REVERSE given then convert from VMS
  4972. to UNIX-ish."
  4973.   (ange-ftp-save-match-data
  4974.     (if reverse
  4975.     (if (string-match "^\\([^:]+:\\)?\\(\\[.*\\]\\)?\\([^][]*\\)$" path)
  4976.         (let (drive dir file)
  4977.           (if (match-beginning 1)
  4978.           (setq drive (substring path
  4979.                      (match-beginning 1)
  4980.                      (match-end 1))))
  4981.           (if (match-beginning 2)
  4982.           (setq dir
  4983.             (substring path (match-beginning 2) (match-end 2))))
  4984.           (if (match-beginning 3)
  4985.           (setq file
  4986.             (substring path (match-beginning 3) (match-end 3))))
  4987.           (and dir
  4988.            (setq dir (apply (function concat)
  4989.                     (mapcar (function
  4990.                          (lambda (char)
  4991.                            (if (= char ?.)
  4992.                            (vector ?/)
  4993.                          (vector char))))
  4994.                         (substring dir 1 -1)))))
  4995.           (concat (and drive
  4996.                (concat "/" drive "/"))
  4997.               dir (and dir "/")
  4998.               file))
  4999.       (error "path %s didn't match" path))
  5000.       (let (drive dir file tmp)
  5001.     (if (string-match "^/[^:]+:/" path)
  5002.         (setq drive (substring path 1
  5003.                    (1- (match-end 0)))
  5004.           path (substring path (match-end 0))))
  5005.     (setq tmp (file-name-directory path))
  5006.     (if tmp
  5007.         (setq dir (apply (function concat)
  5008.                  (mapcar (function
  5009.                       (lambda (char)
  5010.                     (if (= char ?/)
  5011.                         (vector ?.)
  5012.                       (vector char))))
  5013.                      (substring tmp 0 -1)))))
  5014.     (setq file (file-name-nondirectory path))
  5015.     (concat drive
  5016.         (and dir (concat "[" (if drive nil ".") dir "]"))
  5017.         file)))))
  5018.  
  5019. ;; (ange-ftp-fix-path-for-vms "/PUB$:/ANONYMOUS/SDSCPUB/NEXT/Readme.txt;1")
  5020. ;; (ange-ftp-fix-path-for-vms "/PUB$:[ANONYMOUS.SDSCPUB.NEXT]Readme.txt;1" t)
  5021.  
  5022. (or (assq 'vms ange-ftp-fix-path-func-alist)
  5023.     (setq ange-ftp-fix-path-func-alist
  5024.       (cons '(vms . ange-ftp-fix-path-for-vms)
  5025.         ange-ftp-fix-path-func-alist)))
  5026.  
  5027. (or (memq 'vms ange-ftp-dumb-host-types)
  5028.     (setq ange-ftp-dumb-host-types
  5029.       (cons 'vms ange-ftp-dumb-host-types)))
  5030.  
  5031. ;; It is important that this function barf for directories for which we know
  5032. ;; that we cannot possibly get a directory listing, such as "/" and "/DEV:/".
  5033. ;; This is because it saves an unnecessary FTP error, or possibly the listing
  5034. ;; might succeed, but give erroneous info. This last case is particularly
  5035. ;; likely for OS's (like MTS) for which we need to use a wildcard in order
  5036. ;; to list a directory.
  5037.  
  5038. (defun ange-ftp-fix-dir-path-for-vms (dir-path)
  5039.   "Convert path from UNIX-ish to VMS ready for a DIRectory listing."
  5040.   ;; Should there be entries for .. -> [-] and . -> [] below. Don't
  5041.   ;; think so, because expand-filename should have already short-circuited
  5042.   ;; them.
  5043.   (cond ((string-equal dir-path "/")
  5044.      (error "Cannot get listing for fictitious \"/\" directory."))
  5045.     ((string-match "^/[-A-Z0-9_$]+:/$" dir-path)
  5046.      (error "Cannot get listing for device."))
  5047.     ((ange-ftp-fix-path-for-vms dir-path))))
  5048.   
  5049. (or (assq 'vms ange-ftp-fix-dir-path-func-alist)
  5050.     (setq ange-ftp-fix-dir-path-func-alist
  5051.       (cons '(vms . ange-ftp-fix-dir-path-for-vms)
  5052.         ange-ftp-fix-dir-path-func-alist)))
  5053.  
  5054. (defvar ange-ftp-vms-host-regexp nil)
  5055.  
  5056. (defun ange-ftp-vms-host (host)
  5057.   "Return whether HOST is running VMS."
  5058.   (and ange-ftp-vms-host-regexp
  5059.        (ange-ftp-save-match-data
  5060.      (string-match ange-ftp-vms-host-regexp host))))
  5061.  
  5062. ;; Because some VMS ftp servers convert filenames to lower case
  5063. ;; we allow a-z in the filename regexp. I'm not too happy about this.
  5064.  
  5065. (defconst ange-ftp-vms-filename-regexp
  5066.   (concat
  5067.    "\\(\\([_A-Za-z0-9$]?\\|[_A-Za-z0-9$][_A-Za-z0-9$---]*\\)\\."
  5068.    "[_A-Za-z0-9$---]*;+[0-9]*\\)")
  5069.   "Regular expression to match for a valid VMS file name in Dired buffer.
  5070. Stupid freaking bug! Position of _ and $ shouldn't matter but they do.
  5071. Having [A-Z0-9$_] bombs on filename _$$CHANGE_LOG$.TXT$ and $CHANGE_LOG$.TX
  5072. Other orders of $ and _ seem to all work just fine.")
  5073.  
  5074. ;; These parsing functions are as general as possible because the syntax
  5075. ;; of ftp listings from VMS hosts is a bit erratic. What saves us is that
  5076. ;; the VMS filename syntax is so rigid. If they bomb on a listing in the
  5077. ;; standard VMS Multinet format, then this is a bug. If they bomb on a listing
  5078. ;; from vms.weird.net, then too bad.
  5079.  
  5080. (defun ange-ftp-parse-vms-filename ()
  5081.   "Extract the next filename from a VMS dired-like listing."
  5082.   (if (re-search-forward
  5083.      ange-ftp-vms-filename-regexp
  5084.      nil t)
  5085.     (buffer-substring (match-beginning 0) (match-end 0))))
  5086.  
  5087. (defun ange-ftp-parse-vms-listing ()
  5088.   "Parse the current buffer which is assumed to be in MultiNet FTP dir
  5089. format, and return a hashtable as the result."
  5090.   (let ((tbl (ange-ftp-make-hashtable))
  5091.     file)
  5092.     (goto-char (point-min))
  5093.     (ange-ftp-save-match-data
  5094.       (while (setq file (ange-ftp-parse-vms-filename))
  5095.     (if (string-match "\\.\\(DIR\\|dir\\);[0-9]+" file)
  5096.         ;; deal with directories
  5097.         (ange-ftp-put-hash-entry
  5098.          (substring file 0 (match-beginning 0)) t tbl)
  5099.       (ange-ftp-put-hash-entry file nil tbl)
  5100.       (if (string-match ";[0-9]+$" file) ; deal with extension
  5101.           ;; sans extension
  5102.           (ange-ftp-put-hash-entry
  5103.            (substring file 0 (match-beginning 0)) nil tbl)))
  5104.     (forward-line 1))
  5105.       ;; Would like to look for a "Total" line, or a "Directory" line to
  5106.       ;; make sure that the listing isn't complete garbage before putting
  5107.       ;; in "." and "..", but we can't even count on all VAX's giving us
  5108.       ;; either of these.
  5109.        (ange-ftp-put-hash-entry "." t tbl)
  5110.        (ange-ftp-put-hash-entry ".." t tbl))
  5111.     tbl))
  5112.  
  5113. (or (assq 'vms ange-ftp-parse-list-func-alist)
  5114.     (setq ange-ftp-parse-list-func-alist
  5115.       (cons '(vms . ange-ftp-parse-vms-listing)
  5116.         ange-ftp-parse-list-func-alist)))
  5117.  
  5118. ;; This version only deletes file entries which have
  5119. ;; explicit version numbers, because that is all VMS allows.
  5120.  
  5121. ;; Can the following two functions be speeded up using file
  5122. ;; completion functions?
  5123.  
  5124. (defun ange-ftp-vms-delete-file-entry (path &optional dir-p)
  5125.   (if dir-p
  5126.       (ange-ftp-internal-delete-file-entry path t)
  5127.     (ange-ftp-save-match-data
  5128.       (let ((file (ange-ftp-get-file-part path)))
  5129.     (if (string-match ";[0-9]+$" file)
  5130.         ;; In VMS you can't delete a file without an explicit    
  5131.         ;; version number, or wild-card (e.g. FOO;*)
  5132.         ;; For now, we give up on wildcards.
  5133.         (let ((files (ange-ftp-get-hash-entry
  5134.               (file-name-directory path)
  5135.               ange-ftp-files-hashtable)))
  5136.           (if files
  5137.           (let* ((root (substring file 0
  5138.                       (match-beginning 0)))
  5139.              (regexp (concat "^"
  5140.                      (regexp-quote root)
  5141.                      ";[0-9]+$"))
  5142.              versions)
  5143.             (ange-ftp-del-hash-entry file files)
  5144.             ;; Now we need to check if there are any
  5145.             ;; versions left. If not, then delete the
  5146.             ;; root entry.
  5147.             (mapatoms
  5148.              '(lambda (sym)
  5149.             (and (string-match regexp (get sym 'key))
  5150.                  (setq versions t)))
  5151.              files)
  5152.             (or versions
  5153.             (ange-ftp-del-hash-entry root files))))))))))
  5154.  
  5155. (or (assq 'vms ange-ftp-delete-file-entry-alist)
  5156.     (setq ange-ftp-delete-file-entry-alist
  5157.       (cons '(vms . ange-ftp-vms-delete-file-entry)
  5158.         ange-ftp-delete-file-entry-alist)))
  5159.  
  5160. (defun ange-ftp-vms-add-file-entry (path &optional dir-p)
  5161.   (if dir-p
  5162.       (ange-ftp-internal-add-file-entry path t)
  5163.     (let ((files (ange-ftp-get-hash-entry
  5164.           (file-name-directory path)
  5165.           ange-ftp-files-hashtable)))
  5166.       (if files
  5167.       (let ((file (ange-ftp-get-file-part path)))
  5168.         (ange-ftp-save-match-data
  5169.           (if (string-match ";[0-9]+$" file)
  5170.           (ange-ftp-put-hash-entry
  5171.            (substring file 0 (match-beginning 0))
  5172.            nil files)
  5173.         ;; Need to figure out what version of the file
  5174.         ;; is being added.
  5175.         (let ((regexp (concat "^"
  5176.                       (regexp-quote file)
  5177.                       ";\\([0-9]+\\)$"))
  5178.               (version 0))
  5179.           (mapatoms
  5180.            '(lambda (sym)
  5181.               (let ((name (get sym 'key)))
  5182.             (and (string-match regexp name)
  5183.                  (setq version
  5184.                    (max version
  5185.                     (string-to-int
  5186.                      (substring name
  5187.                             (match-beginning 1)
  5188.                             (match-end 1))))))))
  5189.            files)
  5190.           (setq version (1+ version))
  5191.           (ange-ftp-put-hash-entry
  5192.            (concat file ";" (int-to-string version))
  5193.            nil files))))
  5194.         (ange-ftp-put-hash-entry file nil files))))))
  5195.  
  5196. (or (assq 'vms ange-ftp-add-file-entry-alist)
  5197.     (setq ange-ftp-add-file-entry-alist
  5198.       (cons '(vms . ange-ftp-vms-add-file-entry)
  5199.         ange-ftp-add-file-entry-alist)))
  5200.  
  5201.  
  5202. (defun ange-ftp-add-vms-host (host)
  5203.   "Interactively adds a given HOST to ange-ftp-vms-host-regexp."
  5204.   (interactive
  5205.    (list (read-string "Host: "
  5206.               (let ((name (or (buffer-file-name)
  5207.                       (and (eq major-mode 'dired-mode)
  5208.                        dired-directory))))
  5209.             (and name (car (ange-ftp-ftp-path name)))))))
  5210.   (if (not (ange-ftp-vms-host host))
  5211.       (setq ange-ftp-vms-host-regexp
  5212.         (concat "^" (regexp-quote host) "$"
  5213.             (and ange-ftp-vms-host-regexp "\\|")
  5214.             ange-ftp-vms-host-regexp)
  5215.         ange-ftp-host-cache nil)))
  5216.  
  5217.  
  5218. (defun ange-ftp-vms-file-name-as-directory (name)
  5219.   (ange-ftp-save-match-data
  5220.     (if (string-match "\\.\\(DIR\\|dir\\)\\(;[0-9]+\\)?$" name)
  5221.     (setq name (substring name 0 (match-beginning 0))))
  5222.     (ange-ftp-real-file-name-as-directory name)))
  5223.  
  5224. (or (assq 'vms ange-ftp-file-name-as-directory-alist)
  5225.     (setq ange-ftp-file-name-as-directory-alist
  5226.       (cons '(vms . ange-ftp-vms-file-name-as-directory)
  5227.         ange-ftp-file-name-as-directory-alist)))
  5228.  
  5229. ;;; Tree dired support:
  5230.  
  5231. ;; For this code I have borrowed liberally from Sebastian Kremer's
  5232. ;; dired-vms.el
  5233.  
  5234.  
  5235. ;; These regexps must be anchored to beginning of line.
  5236. ;; Beware that the ftpd may put the device in front of the filename.
  5237.  
  5238. (defconst ange-ftp-dired-vms-re-exe "^. [^ \t.]+\\.\\(EXE\\|exe\\)[; ]"
  5239.   "Regular expression to use to search for VMS executable files.")
  5240.  
  5241. (defconst ange-ftp-dired-vms-re-dir "^. [^ \t.]+\\.\\(DIR\\|dir\\)[; ]"
  5242.   "Regular expression to use to search for VMS directories.")
  5243.  
  5244. (or (assq 'vms ange-ftp-dired-re-exe-alist)
  5245.     (setq ange-ftp-dired-re-exe-alist
  5246.       (cons (cons 'vms  ange-ftp-dired-vms-re-exe)
  5247.         ange-ftp-dired-re-exe-alist)))
  5248.  
  5249. (or (assq 'vms ange-ftp-dired-re-dir-alist)
  5250.     (setq ange-ftp-dired-re-dir-alist
  5251.       (cons (cons 'vms  ange-ftp-dired-vms-re-dir)
  5252.         ange-ftp-dired-re-dir-alist)))
  5253.  
  5254. (defun ange-ftp-dired-vms-insert-headerline (dir)
  5255.   ;; VMS inserts a headerline. I would prefer the headerline
  5256.   ;; to be in ange-ftp format. This version tries to
  5257.   ;; be careful, because we can't count on a headerline
  5258.   ;; over ftp, and we wouldn't want to delete anything
  5259.   ;; important.
  5260.   (save-excursion
  5261.     (if (looking-at "^  wildcard ")
  5262.     (forward-line 1))
  5263.     (if (looking-at "^[ \n\t]*[^\n]+\\][ \t]*\n")
  5264.     (delete-region (point) (match-end 0))))
  5265.   (ange-ftp-real-dired-insert-headerline dir))
  5266.  
  5267. (or (assq 'vms ange-ftp-dired-insert-headerline-alist)
  5268.     (setq ange-ftp-dired-insert-headerline-alist
  5269.       (cons '(vms . ange-ftp-dired-vms-insert-headerline)
  5270.         ange-ftp-dired-insert-headerline-alist)))
  5271.  
  5272. (defun ange-ftp-dired-vms-move-to-filename (&optional raise-error eol)
  5273.   "In dired, move to first char of filename on this line.
  5274. Returns position (point) or nil if no filename on this line."
  5275.   ;; This is the VMS version.
  5276.   (let (case-fold-search)
  5277.     (or eol (setq eol (progn (end-of-line) (point))))
  5278.     (beginning-of-line)
  5279.     (if (re-search-forward ange-ftp-vms-filename-regexp eol t)
  5280.     (goto-char (match-beginning 1))
  5281.       (if raise-error
  5282.       (error "No file on this line")
  5283.     nil))))
  5284.  
  5285. (or (assq 'vms ange-ftp-dired-move-to-filename-alist)
  5286.     (setq ange-ftp-dired-move-to-filename-alist
  5287.       (cons '(vms . ange-ftp-dired-vms-move-to-filename)
  5288.         ange-ftp-dired-move-to-filename-alist)))
  5289.  
  5290. (defun ange-ftp-dired-vms-move-to-end-of-filename (&optional no-error eol)
  5291.   ;; Assumes point is at beginning of filename.
  5292.   ;; So, it should be called only after (dired-move-to-filename t).
  5293.   ;; case-fold-search must be nil, at least for VMS.
  5294.   ;; On failure, signals an error or returns nil.
  5295.   ;; This is the VMS version.
  5296.   (let (opoint hidden case-fold-search)
  5297.     (setq opoint (point))
  5298.     (or eol (setq eol (save-excursion (end-of-line) (point))))
  5299.     (setq hidden (and selective-display
  5300.               (save-excursion (search-forward "\r" eol t))))
  5301.     (if hidden
  5302.     nil
  5303.       (re-search-forward ange-ftp-vms-filename-regexp eol t))
  5304.     (or no-error
  5305.     (not (eq opoint (point)))
  5306.     (error
  5307.      (if hidden
  5308.          (substitute-command-keys
  5309.           "File line is hidden, type \\[dired-hide-subdir] to unhide")
  5310.        "No file on this line")))
  5311.     (if (eq opoint (point))
  5312.     nil
  5313.       (point))))
  5314.  
  5315. (or (assq 'vms ange-ftp-dired-move-to-end-of-filename-alist)
  5316.     (setq ange-ftp-dired-move-to-end-of-filename-alist
  5317.       (cons '(vms . ange-ftp-dired-vms-move-to-end-of-filename)
  5318.         ange-ftp-dired-move-to-end-of-filename-alist)))
  5319.  
  5320. (defun ange-ftp-dired-vms-between-files ()
  5321.   (save-excursion
  5322.     (beginning-of-line)
  5323.     (or (equal (following-char) 10) ; newline
  5324.      (equal (following-char) 9)     ; tab
  5325.      (progn (forward-char 2)
  5326.         (or (looking-at "Total of")
  5327.         (equal (following-char) 32))))))
  5328.  
  5329. (or (assq 'vms ange-ftp-dired-between-files-alist)
  5330.     (setq ange-ftp-dired-between-files-alist
  5331.       (cons '(vms . ange-ftp-dired-vms-between-files)
  5332.         ange-ftp-dired-between-files-alist)))
  5333.  
  5334. ;; Beware! In VMS filenames must be of the form "FILE.TYPE".
  5335. ;; Therefore, we cannot just append a ".Z" to filenames for
  5336. ;; compressed files. Instead, we turn "FILE.TYPE" into
  5337. ;; "FILE.TYPE-Z". Hope that this is a reasonable thing to do.
  5338.  
  5339. (defun ange-ftp-vms-make-compressed-filename (name &optional reverse)
  5340.   (if reverse
  5341.       (cond
  5342.        ((string-match "-Z;[0-9]+$" name)
  5343.     (substring name 0 (match-beginning 0)))
  5344.        ((string-match ";[0-9]+$" name)
  5345.     (substring name 0 (match-beginning 0)))
  5346.        ((string-match "-Z$" name)
  5347.     (substring name 0 -2))
  5348.        (t name))
  5349.     (if (string-match ";[0-9]+$" name)
  5350.     (concat (substring name 0 (match-beginning 0))
  5351.         "-Z")
  5352.       (concat name "-Z"))))
  5353.  
  5354. (or (assq 'vms ange-ftp-dired-compress-make-compressed-filename-alist)
  5355.     (setq ange-ftp-dired-compress-make-compressed-filename-alist
  5356.       (cons '(vms . ange-ftp-vms-make-compressed-filename)
  5357.         ange-ftp-dired-compress-make-compressed-filename-alist)))
  5358.  
  5359. ;; When the filename is too long, VMS will use two lines to list a file
  5360. ;; (damn them!) This will confuse dired. To solve this, need to convince
  5361. ;; Sebastian to use a function dired-go-to-end-of-file-line, instead of
  5362. ;; (forward-line 1). This would require a number of changes to dired.el.
  5363. ;; If dired gets confused, revert-buffer will fix it.
  5364.  
  5365. (defun ange-ftp-dired-vms-ls-trim ()
  5366.   (goto-char (point-min))
  5367.   (let ((case-fold-search nil))
  5368.     (re-search-forward  ange-ftp-vms-filename-regexp))
  5369.   (beginning-of-line)
  5370.   (delete-region (point-min) (point))
  5371.   (forward-line 1)
  5372.   (delete-region (point) (point-max)))
  5373.  
  5374.  
  5375. (or (assq 'vms ange-ftp-dired-ls-trim-alist)
  5376.     (setq ange-ftp-dired-ls-trim-alist
  5377.       (cons '(vms . ange-ftp-dired-vms-ls-trim)
  5378.         ange-ftp-dired-ls-trim-alist)))    
  5379.  
  5380. (defun ange-ftp-vms-bob-version (name)
  5381.   (ange-ftp-save-match-data
  5382.     (if (string-match ";[0-9]+$" name)
  5383.     (substring name 0 (match-beginning 0))
  5384.       name)))
  5385.  
  5386. (or (assq 'vms ange-ftp-bob-version-alist)
  5387.     (setq ange-ftp-bob-version-alist
  5388.       (cons '(vms . ange-ftp-vms-bob-version)
  5389.         ange-ftp-bob-version-alist)))
  5390.  
  5391. ;;; The vms version of clean-directory has 2 more optional args
  5392. ;;; than the usual dired version. This is so that it can be used by
  5393. ;;; ange-ftp-dired-vms-flag-backup-files.
  5394.  
  5395. (defun ange-ftp-dired-vms-clean-directory (keep &optional marker msg)
  5396.   "Flag numerical backups for deletion.
  5397. Spares `dired-kept-versions' latest versions, and `kept-old-versions' oldest.
  5398. Positive prefix arg KEEP overrides `dired-kept-versions';
  5399. Negative prefix arg KEEP overrides `kept-old-versions' with KEEP made positive.
  5400.  
  5401. To clear the flags on these files, you can use \\[dired-flag-backup-files]
  5402. with a prefix argument."
  5403. ;  (interactive "P") ; Never actually called interactively.
  5404.   (setq keep (max 1 (if keep (prefix-numeric-value keep) dired-kept-versions)))
  5405.   (let ((early-retention (if (< keep 0) (- keep) kept-old-versions))
  5406.     ;; late-retention must NEVER be allowed to be less than 1 in VMS!
  5407.     ;; This could wipe ALL copies of the file.
  5408.     (late-retention (max 1 (if (<= keep 0) dired-kept-versions keep)))
  5409.     (action (or msg "Cleaning"))
  5410.     (trample-marker (or marker dired-del-marker))
  5411.     (file-version-assoc-list ()))
  5412.     (message (concat action
  5413.              " numerical backups (keeping %d late, %d old)...")
  5414.          late-retention early-retention)
  5415.     ;; Look at each file.
  5416.     ;; If the file has numeric backup versions,
  5417.     ;; put on file-version-assoc-list an element of the form
  5418.     ;; (FILENAME . VERSION-NUMBER-LIST)
  5419.     (dired-map-dired-file-lines (function
  5420.                  ange-ftp-dired-vms-collect-file-versions))
  5421.     ;; Sort each VERSION-NUMBER-LIST,
  5422.     ;; and remove the versions not to be deleted.
  5423.     (let ((fval file-version-assoc-list))
  5424.       (while fval
  5425.     (let* ((sorted-v-list (cons 'q (sort (cdr (car fval)) '<)))
  5426.            (v-count (length sorted-v-list)))
  5427.       (if (> v-count (+ early-retention late-retention))
  5428.           (rplacd (nthcdr early-retention sorted-v-list)
  5429.               (nthcdr (- v-count late-retention)
  5430.                   sorted-v-list)))
  5431.       (rplacd (car fval)
  5432.           (cdr sorted-v-list)))
  5433.     (setq fval (cdr fval))))
  5434.     ;; Look at each file.  If it is a numeric backup file,
  5435.     ;; find it in a VERSION-NUMBER-LIST and maybe flag it for deletion.
  5436.     (dired-map-dired-file-lines
  5437.      (function
  5438.       ange-ftp-dired-vms-trample-file-versions mark))
  5439.     (message (concat action " numerical backups...done"))))
  5440.  
  5441. (or (assq 'vms ange-ftp-dired-clean-directory-alist)
  5442.     (setq ange-ftp-dired-clean-directory-alist
  5443.       (cons '(vms . ange-ftp-dired-vms-clean-directory)
  5444.         ange-ftp-dired-clean-directory-alist)))
  5445.  
  5446. (defun ange-ftp-dired-vms-collect-file-versions (fn)
  5447.   ;;  "If it looks like file FN has versions, return a list of the versions.
  5448.   ;;That is a list of strings which are file names.
  5449.   ;;The caller may want to flag some of these files for deletion."
  5450. (let ((path (nth 2 (ange-ftp-ftp-path fn))))
  5451.   (if (string-match ";[0-9]+$" path)
  5452.       (let* ((path (substring path 0 (match-beginning 0)))
  5453.          (fn (ange-ftp-replace-path-component fn path)))
  5454.     (if (not (assq fn file-version-assoc-list))
  5455.         (let* ((base-versions
  5456.             (concat (file-name-nondirectory path) ";"))
  5457.            (bv-length (length base-versions))
  5458.            (possibilities (file-name-all-completions
  5459.                    base-versions
  5460.                    (file-name-directory fn)))
  5461.            (versions (mapcar
  5462.                   '(lambda (arg)
  5463.                  (if (and (string-match
  5464.                        "[0-9]+$" arg bv-length)
  5465.                       (= (match-beginning 0) bv-length))
  5466.                      (string-to-int (substring arg bv-length))
  5467.                    0))
  5468.                   possibilities)))
  5469.           (if versions
  5470.           (setq
  5471.            file-version-assoc-list
  5472.            (cons (cons fn versions)
  5473.              file-version-assoc-list)))))))))
  5474.  
  5475. (defun ange-ftp-dired-vms-trample-file-versions (fn)
  5476.   (let* ((start-vn (string-match ";[0-9]+$" fn))
  5477.      base-version-list)
  5478.     (and start-vn
  5479.      (setq base-version-list    ; there was a base version to which
  5480.            (assoc (substring fn 0 start-vn)    ; this looks like a
  5481.               file-version-assoc-list))    ; subversion
  5482.      (not (memq (string-to-int (substring fn (1+ start-vn)))
  5483.             base-version-list))    ; this one doesn't make the cut
  5484.      (progn (beginning-of-line)
  5485.         (delete-char 1)
  5486.         (insert trample-marker)))))
  5487.  
  5488. (defun ange-ftp-dired-vms-flag-backup-files (&optional unflag-p)
  5489.   (let ((dired-kept-versions 1)
  5490.     (kept-old-versions 0)
  5491.     marker msg)
  5492.     (if unflag-p
  5493.     (setq marker ?\040 msg "Unflagging")
  5494.       (setq marker dired-del-marker msg "Cleaning"))
  5495.     (ange-ftp-dired-vms-clean-directory nil marker msg)))
  5496.  
  5497. (or (assq 'vms ange-ftp-dired-flag-backup-files-alist)
  5498.     (setq ange-ftp-dired-flag-backup-files-alist
  5499.       (cons '(vms . ange-ftp-dired-vms-flag-backup-files)
  5500.         ange-ftp-dired-flag-backup-files-alist)))
  5501.  
  5502. (defun ange-ftp-dired-vms-backup-diff (&optional switches)
  5503.   (let ((file (dired-get-filename 'no-dir))
  5504.     bak)
  5505.     (if (and (string-match ";[0-9]+$" file)
  5506.          ;; Find most recent previous version.
  5507.          (let ((root (substring file 0 (match-beginning 0)))
  5508.            (ver
  5509.             (string-to-int (substring file (1+ (match-beginning 0)))))
  5510.            found)
  5511.            (setq ver (1- ver))
  5512.            (while (and (> ver 0) (not found))
  5513.          (setq bak (concat root ";" (int-to-string ver)))
  5514.          (and (file-exists-p bak) (setq found t))
  5515.          (setq ver (1- ver)))
  5516.            found))
  5517.     (if switches
  5518.         (diff (expand-file-name bak) (expand-file-name file) switches)
  5519.       (diff (expand-file-name bak) (expand-file-name file)))
  5520.       (error "No previous version found for %s" file))))
  5521.  
  5522. (or (assq 'vms ange-ftp-dired-backup-diff-alist)
  5523.     (setq ange-ftp-dired-backup-diff-alist
  5524.       (cons '(vms . ange-ftp-dired-vms-backup-diff)
  5525.         ange-ftp-dired-backup-diff-alist)))
  5526.  
  5527.  
  5528. ;;;; ------------------------------------------------------------
  5529. ;;;; MTS support
  5530. ;;;; ------------------------------------------------------------
  5531.  
  5532.  
  5533. (defun ange-ftp-fix-path-for-mts (path &optional reverse)
  5534.   "Convert PATH from UNIX-ish to MTS. If REVERSE given then convert from
  5535. MTS to UNIX-ish."
  5536.   (ange-ftp-save-match-data
  5537.     (if reverse
  5538.     (if (string-match "^\\([^:]+:\\)?\\(.*\\)$" path)
  5539.         (let (acct file)
  5540.           (if (match-beginning 1)
  5541.           (setq acct (substring path 0 (match-end 1))))
  5542.           (if (match-beginning 2)
  5543.           (setq file (substring path
  5544.                     (match-beginning 2) (match-end 2))))
  5545.           (concat (and acct (concat "/" acct "/"))
  5546.               file))
  5547.       (error "path %s didn't match" path))
  5548.       (if (string-match "^/\\([^:]+:\\)/\\(.*\\)$" path)
  5549.       (concat (substring path 1 (match-end 1))
  5550.           (substring path (match-beginning 2) (match-end 2)))
  5551.     ;; Let's hope that mts will recognize it anyway.
  5552.     path))))
  5553.  
  5554. (or (assq 'mts ange-ftp-fix-path-func-alist)
  5555.     (setq ange-ftp-fix-path-func-alist
  5556.       (cons '(mts . ange-ftp-fix-path-for-mts)
  5557.         ange-ftp-fix-path-func-alist)))
  5558.  
  5559. (defun ange-ftp-fix-dir-path-for-mts (dir-path)
  5560.   "Convert path from UNIX-ish to MTS ready for a DIRectory listing.
  5561. Remember that there are no directories in MTS."
  5562.   (if (string-equal dir-path "/")
  5563.       (error "Cannot get listing for fictitious \"/\" directory.")
  5564.     (let ((dir-path (ange-ftp-fix-path-for-mts dir-path)))
  5565.       (cond
  5566.        ((string-equal dir-path "")
  5567.     "?")
  5568.        ((string-match ":$" dir-path)
  5569.     (concat dir-path "?"))
  5570.        (dir-path))))) ; It's just a single file.
  5571.  
  5572. (or (assq 'mts ange-ftp-fix-dir-path-func-alist)
  5573.     (setq ange-ftp-fix-dir-path-func-alist
  5574.       (cons '(mts . ange-ftp-fix-dir-path-for-mts)
  5575.         ange-ftp-fix-dir-path-func-alist)))
  5576.  
  5577. (or (memq 'mts ange-ftp-dumb-host-types)
  5578.     (setq ange-ftp-dumb-host-types
  5579.       (cons 'mts ange-ftp-dumb-host-types)))
  5580.  
  5581. (defvar ange-ftp-mts-host-regexp nil)
  5582.  
  5583. (defun ange-ftp-mts-host (host)
  5584.   "Return whether HOST is running MTS."
  5585.   (and ange-ftp-mts-host-regexp
  5586.        (ange-ftp-save-match-data
  5587.      (string-match ange-ftp-mts-host-regexp host))))
  5588.  
  5589. (defun ange-ftp-parse-mts-listing ()
  5590.   "Parse the current buffer which is assumed to be in
  5591. mts ftp dir format."
  5592.   (let ((tbl (ange-ftp-make-hashtable)))
  5593.     (goto-char (point-min))
  5594.     (ange-ftp-save-match-data
  5595.       (while (re-search-forward ange-ftp-date-regexp nil t)
  5596.     (end-of-line)
  5597.     (skip-chars-backward " ")
  5598.     (let ((end (point)))
  5599.       (skip-chars-backward "-A-Z0-9_.!")
  5600.       (ange-ftp-put-hash-entry (buffer-substring (point) end) nil tbl))
  5601.     (forward-line 1)))
  5602.       ;; Don't need to bother with ..
  5603.     (ange-ftp-put-hash-entry "." t tbl)
  5604.     tbl))
  5605.  
  5606. (or (assq 'mts ange-ftp-parse-list-func-alist)
  5607.     (setq ange-ftp-parse-list-func-alist
  5608.       (cons '(mts . ange-ftp-parse-mts-listing)
  5609.         ange-ftp-parse-list-func-alist)))
  5610.  
  5611. (defun ange-ftp-add-mts-host (host)
  5612.   "Interactively adds a given HOST to ange-ftp-mts-host-regexp."
  5613.   (interactive
  5614.    (list (read-string "Host: "
  5615.               (let ((name (or (buffer-file-name)
  5616.                       (and (eq major-mode 'dired-mode)
  5617.                        dired-directory))))
  5618.             (and name (car (ange-ftp-ftp-path name)))))))
  5619.   (if (not (ange-ftp-mts-host host))
  5620.       (setq ange-ftp-mts-host-regexp
  5621.         (concat "^" (regexp-quote host) "$"
  5622.             (and ange-ftp-mts-host-regexp "\\|")
  5623.             ange-ftp-mts-host-regexp)
  5624.         ange-ftp-host-cache nil)))
  5625.  
  5626. ;;; Tree dired support:
  5627.  
  5628. ;; There aren't too many systems left that use MTS. This dired support will
  5629. ;; work for the implementation of ftp on mtsg.ubc.ca. I hope other mts systems
  5630. ;; implement ftp in the same way. If not, it might be necessary to make the
  5631. ;; following more flexible.
  5632.  
  5633. (defun ange-ftp-dired-mts-move-to-filename (&optional raise-error eol)
  5634.   "In dired, move to first char of filename on this line.
  5635. Returns position (point) or nil if no filename on this line."
  5636.   ;; This is the MTS version.
  5637.   (or eol (setq eol (progn (end-of-line) (point))))
  5638.   (beginning-of-line)
  5639.   (if (re-search-forward
  5640.        ange-ftp-date-regexp eol t)
  5641.       (progn
  5642.     (skip-chars-forward " ")          ; Eat blanks after date
  5643.     (skip-chars-forward "0-9:" eol)   ; Eat time or year
  5644.     (skip-chars-forward " " eol)      ; one space before filename
  5645.     ;; When listing an account other than the users own account it appends
  5646.     ;; ACCT: to the beginning of the filename. Skip over this.
  5647.     (and (looking-at "[A-Z0-9_.]+:")
  5648.          (goto-char (match-end 0)))
  5649.     (point))
  5650.     (if raise-error
  5651.     (error "No file on this line")
  5652.       nil)))
  5653.  
  5654. (or (assq 'mts ange-ftp-dired-move-to-filename-alist)
  5655.     (setq ange-ftp-dired-move-to-filename-alist
  5656.       (cons '(mts . ange-ftp-dired-mts-move-to-filename)
  5657.         ange-ftp-dired-move-to-filename-alist)))
  5658.  
  5659. (defun ange-ftp-dired-mts-move-to-end-of-filename (&optional no-error eol)
  5660.   ;; Assumes point is at beginning of filename.
  5661.   ;; So, it should be called only after (dired-move-to-filename t).
  5662.   ;; On failure, signals an error or returns nil.
  5663.   ;; This is the MTS version.
  5664.   (let (opoint hidden case-fold-search)
  5665.     (setq opoint (point)
  5666.       eol (save-excursion (end-of-line) (point))
  5667.       hidden (and selective-display
  5668.               (save-excursion (search-forward "\r" eol t))))
  5669.     (if hidden
  5670.     nil
  5671.       (skip-chars-forward "-A-Z0-9._!" eol))
  5672.     (or no-error
  5673.     (not (eq opoint (point)))
  5674.     (error
  5675.      (if hidden
  5676.          (substitute-command-keys
  5677.           "File line is hidden, type \\[dired-hide-subdir] to unhide")
  5678.        "No file on this line")))
  5679.     (if (eq opoint (point))
  5680.     nil
  5681.       (point))))
  5682.  
  5683. (or (assq 'mts ange-ftp-dired-move-to-end-of-filename-alist)
  5684.     (setq ange-ftp-dired-move-to-end-of-filename-alist
  5685.       (cons '(mts . ange-ftp-dired-mts-move-to-end-of-filename)
  5686.         ange-ftp-dired-move-to-end-of-filename-alist)))
  5687.  
  5688. ;;;; ------------------------------------------------------------
  5689. ;;;; CMS support
  5690. ;;;; ------------------------------------------------------------
  5691.  
  5692. ;; Since CMS doesn't have any full pathname syntax, we have to fudge
  5693. ;; things with cd's. We actually send too many cd's, but is dangerous
  5694. ;; to try to remember the current minidisk, because if the connection
  5695. ;; is closed and needs to be reopened, we will find ourselves back in
  5696. ;; the default minidisk. This is fairly likely since CMS ftp servers
  5697. ;; usually close the connection after 5 minutes of inactivity.
  5698.  
  5699. ;; Have I got the filename character set right?
  5700.  
  5701. (defun ange-ftp-fix-path-for-cms (path &optional reverse)
  5702.   "Convert PATH from UNIX-ish to CMS. If REVERSE is given, convert
  5703. from CMS to UNIX. Actually, CMS doesn't have a full pathname syntax,
  5704. so we fudge things by sending cd's."
  5705.   (ange-ftp-save-match-data
  5706.     (if reverse
  5707.     ;; Since we only convert output from a pwd in this direction,
  5708.     ;; we'll assume that it's a minidisk, and make it into a
  5709.     ;; directory file name. Note that the expand-dir-hashtable
  5710.     ;; stores directories without the trailing /. Is this
  5711.     ;; consistent?
  5712.     (concat "/" path)
  5713.       (if (string-match "^/\\([-A-Z0-9$*._]+\\)/\\([-A-Z0-9$._]+\\)?$"
  5714.             path)
  5715.       (let ((minidisk (substring path 1 (match-end 1))))
  5716.         (if (match-beginning 2)
  5717.         (let ((file (substring path (match-beginning 2)
  5718.                        (match-end 2)))
  5719.               (cmd (concat "cd " minidisk))
  5720.  
  5721.               ;; Note that host and user are bound in the call
  5722.               ;; to ange-ftp-send-cmd
  5723.               (proc (ange-ftp-get-process host user)))
  5724.  
  5725.           ;; Must use ange-ftp-raw-send-cmd here to avoid
  5726.           ;; an infinite loop.
  5727.           (if (car (ange-ftp-raw-send-cmd proc cmd msg))
  5728.               file
  5729.             ;; failed... try ONCE more.
  5730.             (setq proc (ange-ftp-get-process host user))
  5731.             (let ((result (ange-ftp-raw-send-cmd proc cmd msg)))
  5732.               (if (car result)
  5733.               file
  5734.             ;; failed.  give up.
  5735.             (ange-ftp-error host user
  5736.                     (format "cd to minidisk %s failed: %s"
  5737.                         minidisk (cdr result)))))))
  5738.           ;; return the minidisk
  5739.           minidisk))
  5740.     (error "Invalid CMS filename")))))
  5741.  
  5742. (or (assq 'cms ange-ftp-fix-path-func-alist)
  5743.     (setq ange-ftp-fix-path-func-alist
  5744.       (cons '(cms . ange-ftp-fix-path-for-cms)
  5745.         ange-ftp-fix-path-func-alist)))
  5746.  
  5747. (or (memq 'cms ange-ftp-dumb-host-types)
  5748.     (setq ange-ftp-dumb-host-types
  5749.       (cons 'cms ange-ftp-dumb-host-types)))
  5750.  
  5751. (defun ange-ftp-fix-dir-path-for-cms (dir-path)
  5752.   "Convert path from UNIX-ish to VMS ready for a DIRectory listing."
  5753.   (cond
  5754.    ((string-equal "/" dir-path)
  5755.     (error "Cannot get listing for fictitious \"/\" directory."))
  5756.    ((string-match "^/\\([-A-Z0-9$*._]+\\)/\\([-A-Z0-9$._]+\\)?$" dir-path)
  5757.     (let* ((minidisk (substring dir-path (match-beginning 1) (match-end 1)))
  5758.        ;; host and user are bound in the call to ange-ftp-send-cmd
  5759.        (proc (ange-ftp-get-process host user))
  5760.        (cmd (concat "cd " minidisk))
  5761.        (file (if (match-beginning 2)
  5762.              ;; it's a single file
  5763.              (substring path (match-beginning 2)
  5764.                 (match-end 2))
  5765.            ;; use the wild-card
  5766.            "*")))
  5767.       (if (car (ange-ftp-raw-send-cmd proc cmd))
  5768.       file
  5769.     ;; try again...
  5770.     (setq proc (ange-ftp-get-process host user))
  5771.     (let ((result (ange-ftp-raw-send-cmd proc cmd)))
  5772.       (if (car result)
  5773.           file
  5774.         ;; give up
  5775.         (ange-ftp-error host user
  5776.                 (format "cd to minidisk %s failed: "
  5777.                     minidisk (cdr result))))))))
  5778.    (t (error "Invalid CMS pathname"))))
  5779.  
  5780. (or (assq 'cms ange-ftp-fix-dir-path-func-alist)
  5781.     (setq ange-ftp-fix-dir-path-func-alist
  5782.       (cons '(cms . ange-ftp-fix-dir-path-for-cms)
  5783.         ange-ftp-fix-dir-path-func-alist)))
  5784.  
  5785. (defvar ange-ftp-cms-host-regexp nil
  5786.   "Regular expression to match hosts running the CMS operating system.")
  5787.  
  5788. (defun ange-ftp-cms-host (host)
  5789.   "Return whether the host is running CMS."
  5790.   (and ange-ftp-cms-host-regexp
  5791.        (ange-ftp-save-match-data
  5792.      (string-match ange-ftp-cms-host-regexp host))))
  5793.  
  5794. (defun ange-ftp-add-cms-host (host)
  5795.   "Interactively adds a given HOST to ange-ftp-cms-host-regexp."
  5796.   (interactive
  5797.    (list (read-string "Host: "
  5798.               (let ((name (or (buffer-file-name)
  5799.                       (and (eq major-mode 'dired-mode)
  5800.                        dired-directory))))
  5801.             (and name (car (ange-ftp-ftp-path name)))))))
  5802.   (if (not (ange-ftp-cms-host host))
  5803.       (setq ange-ftp-cms-host-regexp
  5804.         (concat "^" (regexp-quote host) "$"
  5805.             (and ange-ftp-cms-host-regexp "\\|")
  5806.             ange-ftp-cms-host-regexp)
  5807.         ange-ftp-host-cache nil)))
  5808.  
  5809. (defun ange-ftp-parse-cms-listing ()
  5810.   "Parse the current buffer which is assumed to be a CMS directory listing."
  5811.   ;; If we succeed in getting a listing, then we will assume that the minidisk
  5812.   ;; exists. file is bound by the call to ange-ftp-ls. This doesn't work
  5813.   ;; because ange-ftp doesn't know that the root hashtable has only part of
  5814.   ;; the info. It will assume that if a minidisk isn't in it, then it doesn't
  5815.   ;; exist. It would be nice if completion worked for minidisks, as we
  5816.   ;; discover them.
  5817. ;  (let* ((dir-file (directory-file-name file))
  5818. ;     (root (file-name-directory dir-file))
  5819. ;     (minidisk (ange-ftp-get-file-part dir-file))
  5820. ;     (root-tbl (ange-ftp-get-hash-entry root ange-ftp-files-hashtable)))
  5821. ;    (if root-tbl
  5822. ;    (ange-ftp-put-hash-entry minidisk t root-tbl)
  5823. ;      (setq root-tbl (ange-ftp-make-hashtable))
  5824. ;      (ange-ftp-put-hash-entry minidisk t root-tbl)
  5825. ;      (ange-ftp-put-hash-entry "." t root-tbl)
  5826. ;      (ange-ftp-set-files root root-tbl)))
  5827.   ;; Now do the usual parsing
  5828.   (let ((tbl (ange-ftp-make-hashtable)))
  5829.     (goto-char (point-min))
  5830.     (ange-ftp-save-match-data
  5831.       (while
  5832.       (re-search-forward
  5833.        "^\\([-A-Z0-9$_]+\\) +\\([-A-Z0-9$_]+\\) +[VF] +[0-9]+ " nil t)
  5834.     (ange-ftp-put-hash-entry
  5835.      (concat (buffer-substring (match-beginning 1)
  5836.                    (match-end 1))
  5837.          "."
  5838.          (buffer-substring (match-beginning 2)
  5839.                    (match-end 2)))
  5840.      nil tbl)
  5841.     (forward-line 1))
  5842.       (ange-ftp-put-hash-entry "." t tbl))
  5843.     tbl))
  5844.  
  5845. (or (assq 'cms ange-ftp-parse-list-func-alist)
  5846.     (setq ange-ftp-parse-list-func-alist
  5847.       (cons '(cms . ange-ftp-parse-cms-listing)
  5848.         ange-ftp-parse-list-func-alist)))
  5849.     
  5850. ;;; Tree dired support:
  5851.  
  5852. (defconst ange-ftp-dired-cms-re-exe
  5853.   "^. [-A-Z0-9$_]+ +EXEC "
  5854.   "Regular expression to use to search for CMS executables.")
  5855.  
  5856. (or (assq 'cms ange-ftp-dired-re-exe-alist)
  5857.     (setq ange-ftp-dired-re-exe-alist
  5858.       (cons (cons 'cms  ange-ftp-dired-cms-re-exe)
  5859.         ange-ftp-dired-re-exe-alist)))
  5860.  
  5861.  
  5862. (defun ange-ftp-dired-cms-insert-headerline (dir)
  5863.   ;; CMS has no total line, so we insert a blank line for
  5864.   ;; aesthetics.
  5865.   (insert "\n")
  5866.   (forward-char -1)
  5867.   (ange-ftp-real-dired-insert-headerline dir))
  5868.  
  5869. (or (assq 'cms ange-ftp-dired-insert-headerline-alist)
  5870.     (setq ange-ftp-dired-insert-headerline-alist
  5871.       (cons '(cms . ange-ftp-dired-cms-insert-headerline)
  5872.         ange-ftp-dired-insert-headerline-alist)))
  5873.  
  5874. (defun ange-ftp-dired-cms-move-to-filename (&optional raise-error eol)
  5875.   "In dired, move to the first char of filename on this line."
  5876.   ;; This is the CMS version.
  5877.   (or eol (setq eol (progn (end-of-line) (point))))
  5878.   (let (case-fold-search)
  5879.     (beginning-of-line)
  5880.     (if (re-search-forward " [-A-Z0-9$_]+ +[-A-Z0-9$_]+ +[VF] +[0-9]+ " eol t)
  5881.     (goto-char (1+ (match-beginning 0)))
  5882.       (if raise-error
  5883.       (error "No file on this line")
  5884.     nil))))
  5885.  
  5886. (or (assq 'cms ange-ftp-dired-move-to-filename-alist)
  5887.     (setq ange-ftp-dired-move-to-filename-alist
  5888.       (cons '(cms . ange-ftp-dired-cms-move-to-filename)
  5889.         ange-ftp-dired-move-to-filename-alist)))
  5890.  
  5891. (defun ange-ftp-dired-cms-move-to-end-of-filename (&optional no-error eol)
  5892.   ;; Assumes point is at beginning of filename.
  5893.   ;; So, it should be called only after (dired-move-to-filename t).
  5894.   ;; case-fold-search must be nil, at least for VMS.
  5895.   ;; On failure, signals an error or returns nil.
  5896.   ;; This is the CMS version.
  5897.   (let ((opoint (point))
  5898.     case-fold-search hidden)
  5899.     (or eol (setq eol (save-excursion (end-of-line) (point))))
  5900.     (setq hidden (and selective-display
  5901.               (save-excursion
  5902.             (search-forward "\r" eol t))))
  5903.     (if hidden
  5904.     (if no-error
  5905.         nil
  5906.       (error
  5907.        (substitute-command-keys
  5908.         "File line is hidden, type \\[dired-hide-subdir] to unhide")))
  5909.       (skip-chars-forward "-A-Z0-9$_" eol)
  5910.       (skip-chars-forward " " eol)
  5911.       (skip-chars-forward "-A-Z0-9$_" eol)
  5912.       (if (eq opoint (point))
  5913.       (if no-error
  5914.           nil
  5915.         (error "No file on this line"))
  5916.     (point)))))
  5917.  
  5918. (or (assq 'cms ange-ftp-dired-move-to-end-of-filename-alist)
  5919.     (setq ange-ftp-dired-move-to-end-of-filename-alist
  5920.       (cons '(cms . ange-ftp-dired-cms-move-to-end-of-filename)
  5921.         ange-ftp-dired-move-to-end-of-filename-alist)))
  5922.  
  5923. (defun ange-ftp-cms-make-compressed-filename (name &optional reverse)
  5924.   (if reverse
  5925.       (if (string-match "-Z$" name)
  5926.       (substring name 0 -2)
  5927.     name)
  5928.     (concat name "-Z")))
  5929.  
  5930. (or (assq 'cms ange-ftp-dired-compress-make-compressed-filename-alist)
  5931.     (setq ange-ftp-dired-compress-make-compressed-filename-alist
  5932.       (cons '(cms . ange-ftp-cms-make-compressed-filename)
  5933.         ange-ftp-dired-compress-make-compressed-filename-alist)))
  5934.  
  5935. (defun ange-ftp-dired-cms-get-filename (&optional localp no-error-if-not-filep)
  5936.   (let ((name (ange-ftp-real-dired-get-filename localp no-error-if-not-filep)))
  5937.     (and name
  5938.      (if (string-match "^\\([^ ]+\\) +\\([^ ]+\\)$" name)
  5939.          (concat (substring name 0 (match-end 1))
  5940.              "."
  5941.              (substring name (match-beginning 2) (match-end 2)))
  5942.        name))))
  5943.  
  5944. (or (assq 'cms ange-ftp-dired-get-filename-alist)
  5945.     (setq ange-ftp-dired-get-filename-alist
  5946.       (cons '(cms . ange-ftp-dired-cms-get-filename)
  5947.         ange-ftp-dired-get-filename-alist)))
  5948.  
  5949. ;;;; ------------------------------------------------------------
  5950. ;;;; Finally provide package.
  5951. ;;;; ------------------------------------------------------------
  5952.  
  5953. (provide 'ange-ftp)
  5954.