[comp.emacs] Improvements to find-alternate-file

drw@cullvax.UUCP (Dale Worley) (06/16/87)

About 3/4 of the time that I screw up the filename in C-x C-f (and so
would want to use C-x C-v to get the right file), I have the right
filename, but the wrong directory.  So I hacked find-alternate-file so
that if you type C-u C-x C-v, it will append the current filename
(sans directory) to the directory name that it prompts you for.

(defun find-alternate-file (filename &optional prefix)
  "Find file FILENAME, select its buffer, kill previous buffer.
If the current buffer now contains an empty file that you just visited
\(presumably by mistake), use this command to visit the file you really want.
If second argument is non-nil (or prefix argument is given interactively),
the current filename is appended to FILENAME (with a / if necessary) before
it is selected.  If you visited a file but gave the wrong directory, use this
command with a prefix argument to visit the right file, but you only have to
give the correct directory"
  (interactive "FFind alternate file: 
P")
  (and (buffer-modified-p)
       (not buffer-read-only)
       (not (yes-or-no-p (format "Buffer %s is modified; kill anyway? "
				 (buffer-name))))
       (error "Aborted"))
  (if prefix
      (progn
	(if (not (string-equal (substring filename -1) "/"))
	    (setq filename (concat filename "/")))
	(setq filename (concat filename
			       (file-name-nondirectory buffer-file-name)))))
  (let ((obuf (current-buffer))
	(ofile buffer-file-name)
	(oname (buffer-name)))
    (rename-buffer " **lose**")
    (setq buffer-file-name nil)
    (unwind-protect
	(progn
	  (unlock-buffer)
	  (find-file filename))
      (cond ((eq obuf (current-buffer))
	     (setq buffer-file-name ofile)
	     (lock-buffer)
	     (rename-buffer oname))))
    (kill-buffer obuf)))
--------------------
Dale

-- 
Dale Worley	Cullinet Software		ARPA: cullvax!drw@eddie.mit.edu
UUCP: ...!seismo!harvard!mit-eddie!cullvax!drw
"President Nixon has just lowered the speed of light to 55 mph.  At what
speed can 2 colliding VW's of mass m = (number) produce a 3rd VW?"