+;;; Image mode window-info management.
+
+(defvar image-mode-winprops-alist t)
+(make-variable-buffer-local 'image-mode-winprops-alist)
+
+(defvar image-mode-new-window-functions nil
+ "Special hook run when image data is requested in a new window.
+It is called with one argument, the initial WINPROPS.")
+
+(defun image-mode-winprops (&optional window cleanup)
+ "Return winprops of WINDOW.
+A winprops object has the shape (WINDOW . ALIST)."
+ (cond ((null window)
+ (setq window (selected-window)))
+ ((not (windowp window))
+ (error "Not a window: %s" window)))
+ (when cleanup
+ (setq image-mode-winprops-alist
+ (delq nil (mapcar (lambda (winprop)
+ (if (window-live-p (car-safe winprop))
+ winprop))
+ image-mode-winprops-alist))))
+ (let ((winprops (assq window image-mode-winprops-alist)))
+ ;; For new windows, set defaults from the latest.
+ (unless winprops
+ (setq winprops (cons window
+ (copy-alist (cdar image-mode-winprops-alist))))
+ (run-hook-with-args 'image-mode-new-window-functions winprops))
+ ;; Move window to front.
+ (setq image-mode-winprops-alist
+ (cons winprops (delq winprops image-mode-winprops-alist)))
+ winprops))
+
+(defun image-mode-window-get (prop &optional winprops)
+ (unless (consp winprops) (setq winprops (image-mode-winprops winprops)))
+ (cdr (assq prop (cdr winprops))))
+
+(defsetf image-mode-window-get (prop &optional winprops) (val)
+ `(image-mode-window-put ,prop ,val ,winprops))
+
+(defun image-mode-window-put (prop val &optional winprops)
+ (unless (consp winprops) (setq winprops (image-mode-winprops winprops)))
+ (setcdr winprops (cons (cons prop val)
+ (delq (assq prop (cdr winprops)) (cdr winprops)))))
+
+(defun image-set-window-vscroll (vscroll)
+ (setf (image-mode-window-get 'vscroll) vscroll)
+ (set-window-vscroll (selected-window) vscroll))
+
+(defun image-set-window-hscroll (ncol)
+ (setf (image-mode-window-get 'hscroll) ncol)
+ (set-window-hscroll (selected-window) ncol))
+
+(defun image-mode-reapply-winprops ()
+ ;; When set-window-buffer, set hscroll and vscroll to what they were
+ ;; last time the image was displayed in this window.
+ (when (and (image-get-display-property)
+ (listp image-mode-winprops-alist))
+ (let* ((winprops (image-mode-winprops nil t))
+ (hscroll (image-mode-window-get 'hscroll winprops))
+ (vscroll (image-mode-window-get 'vscroll winprops)))
+ (if hscroll (set-window-hscroll (selected-window) hscroll))
+ (if vscroll (set-window-vscroll (selected-window) vscroll)))))
+
+(defun image-mode-setup-winprops ()
+ ;; Record current scroll settings.
+ (unless (listp image-mode-winprops-alist)
+ (setq image-mode-winprops-alist nil))
+ (add-hook 'window-configuration-change-hook
+ 'image-mode-reapply-winprops nil t))
+