-(defun view-mode-enter (&optional return-to exit-action) "\
-Enter View mode and set up exit from view mode depending on optional arguments.
-If RETURN-TO is non-nil it is added as an element to the buffer local alist
-`view-return-to-alist'.
-Save EXIT-ACTION in buffer local variable `view-exit-action'.
-It should be either nil or a function that takes a buffer as argument.
-This function will be called by `view-mode-exit'.
-
-RETURN-TO is either nil, meaning do nothing when exiting view mode, or
-it has the format (WINDOW OLD-WINDOW . OLD-BUF-INFO).
-WINDOW is a window used for viewing.
-OLD-WINDOW is nil or the window to select after viewing.
-OLD-BUF-INFO tells what to do with WINDOW when exiting. It is one of:
-1) nil Do nothing.
-2) t Delete WINDOW or, if it is the only window, its frame.
+(defun view-return-to-alist-update (buffer &optional item)
+ "Update `view-return-to-alist' of buffer BUFFER.
+Remove from `view-return-to-alist' all entries referencing dead
+windows. Optional argument ITEM non-nil means add ITEM to
+`view-return-to-alist' after purging. For a decsription of items
+that can be added see the RETURN-TO-ALIST argument of the
+function `view-mode-exit'. If `view-return-to-alist' contains an
+entry for the selected window, purge that entry from
+`view-return-to-alist' before adding ITEM."
+ (with-current-buffer buffer
+ (when view-return-to-alist
+ (let* ((list view-return-to-alist)
+ entry entry-window last)
+ (while list
+ (setq entry (car list))
+ (setq entry-window (car entry))
+ (if (and (windowp entry-window)
+ (or (and item (eq entry-window (selected-window)))
+ (not (window-live-p entry-window))))
+ ;; Remove that entry.
+ (if last
+ (setcdr last (cdr list))
+ (setq view-return-to-alist
+ (cdr view-return-to-alist)))
+ ;; Leave entry alone.
+ (setq last entry))
+ (setq list (cdr list)))))
+ ;; Add ITEM.
+ (when item
+ (setq view-return-to-alist
+ (cons item view-return-to-alist)))))
+
+;;;###autoload
+(defun view-mode-enter (&optional return-to exit-action)
+ "Enter View mode and set up exit from view mode depending on optional arguments.
+RETURN-TO non-nil means add RETURN-TO as an element to the buffer
+local alist `view-return-to-alist'. Save EXIT-ACTION in buffer
+local variable `view-exit-action'. It should be either nil or a
+function that takes a buffer as argument. This function will be
+called by `view-mode-exit'.
+
+RETURN-TO is either nil, meaning do nothing when exiting view
+mode, or must have the format (WINDOW OLD-WINDOW . OLD-BUF-INFO).
+WINDOW is the window used for viewing. OLD-WINDOW is nil or the
+window to select after viewing. OLD-BUF-INFO tells what to do
+with WINDOW when exiting. It is one of:
+1) nil Do nothing.
+2) t Delete WINDOW or, if it is the only window and
+ `view-remove-frame-by-deleting' is non-nil, its
+ frame.