-(defun view-mode-exit (&optional return-to-alist exit-action all-win)
- "Exit View mode in various ways, depending on optional arguments.
-RETURN-TO-ALIST, EXIT-ACTION and ALL-WIN determine what to do
-after exit. EXIT-ACTION is nil or a function that is called with
-current buffer as argument.
-
-RETURN-TO-ALIST is an alist that, for some of the windows
-displaying the current buffer, maintains information on what to
-do when exiting those windows. If ALL-WIN is non-nil or the
-variable `view-exits-all-viewing-windows' is non-nil,
-view-mode-exit attempts to restore all windows showing the
-current buffer to their old state. Otherwise, only the selected
-window is affected (provided it is on RETURN-TO-ALIST).
-
-Elements of RETURN-TO-ALIST must have the format
- (WINDOW OLD-WINDOW . OLD-BUF-INFO) where
-
-WINDOW is a window displaying the current buffer and OLD-WINDOW
-is either nil or a window to select after viewing. OLD-BUF-INFO
-provides information on what to do with WINDOW and may be one of:
-1) nil Do nothing.
-2) t Delete WINDOW and, if it is the only window and
- `view-remove-frame-by-deleting' is non-nil, its
- frame.
-3) (OLD-BUF START POINT) Display buffer OLD-BUF with displayed text
- starting at START and point at POINT in WINDOW.
-4) quit-window Do `quit-window' in WINDOW.
-5) keep-frame Like case 2) but do not delete the frame.
-
-If one of the WINDOW in RETURN-TO-ALIST is the selected window
-and the corresponding OLD-WINDOW is a live window, then select
-OLD-WINDOW."
- (when view-mode ; Only do something if in view mode.
- (setq all-win
- (and return-to-alist
- (or all-win view-exits-all-viewing-windows)))
- (let* ((buffer (current-buffer))
- window notlost
- (sel-old (assq (selected-window) return-to-alist))
- (alist (cond
- (all-win ; Try to restore all windows.
- (append return-to-alist nil)) ; Copy.
- (sel-old ; Only selected window.
- (list sel-old))))
- (old-window (if sel-old (car (cdr sel-old)))))
- (if all-win ; Follow chains of old-windows.
- (let ((c (length alist)) a)
- (while (and (> c 0) ; Safety if mutually refering windows.
- (or (not (window-live-p old-window))
- (eq buffer (window-buffer old-window)))
- (setq a (assq old-window alist)))
- (setq c (1- c))
- (setq old-window (car (cdr a))))
- (if (or (zerop c) (not (window-live-p old-window)))
- (setq old-window (selected-window)))))
+;; This is awful because it assumes that the selected window shows the
+;; current buffer when this is called.
+(defun view-mode-exit (&optional exit-only exit-action all-windows)
+ "Exit View mode in various ways.
+If all arguments are nil, remove the current buffer from the
+selected window using the `quit-restore' information associated
+with the selected window. If optional argument ALL-WINDOWS or
+`view-exits-all-viewing-windows' are non-nil, remove the current
+buffer from all windows showing it.
+
+Optional argument EXIT-ONLY non-nil means just exit `view-mode'
+\(unless `view-no-disable-on-exit' is non-nil) but do not change
+the associations of any windows with the current buffer.
+
+EXIT-ACTION, if non-nil, must specify a function that is called
+with the current buffer as argument and is called after disabling
+`view-mode' and removing any associations of windows with the
+current buffer. "
+ (when view-mode
+ (let ((buffer (window-buffer)))