Subject: bug#7730: 24.0.50; `image-mode-fit-frame': add optional FRAME arg

From: "Drew Adams" <[email-masked]>
Date: Fri, 24 Dec 2010 15:45:05 -0800

Here's a possible enhancement to `image-mode-fit-frame'.
These are the differences:
 
1. Added an optional FRAME arg.
 
2. The command only toggles if called interactively (actually, if the
first arg is non-nil).
 
#1 means you can use it for another frame.  #2 means that only
 interactive calls save the previous frame size and alternate between
 fitting and restoring the old size.
 
Also, at the end of the function I changed the separate calls to
`set-frame-width' and `set-frame-height' to a single call to
`set-frame-size'.  I did that because I was seeing that the frame was
not getting resized to fit in some cases.  Dunno if there is a bug
associated with that.

FWIW, in my own use of this, my general command `fit-frame' calls it
for Emacs 23+ if the FRAME is in `image-mode' or `image-minor-mode':

(if (and (fboundp 'image-mode-fit-frame) ; Emacs 23+
         (if (or (null frame) (eq frame (selected-frame)))
             (or (eq major-mode 'image-mode) image-minor-mode)
           (save-window-excursion
             (select-frame frame)
             (or (eq major-mode 'image-mode) image-minor-mode))))
      (fit-frame-to-image (interactive-p) frame)
  ...)

(I also use a different name for the command.)
 
(defun fit-frame-to-image (interactivep &optional frame)
  "Fit FRAME to the current image.
If FRAME is not the selected frame, fit it to its first image.
Interactively, if frame has already been fit to the image, then
 restore the size from before it was fit.
This function assumes that FRAME has only one window."
  ;; FIXME: This does not take into account decorations
  ;; like mode-line, minibuffer, header-line, ...
  (interactive "p")
  (let* ((saved    (frame-parameter frame 'image-mode-saved-size))
         (display  (if (or (null frame) (equal frame (selected-frame)))
                       (image-get-display-property)
                     (save-selected-window (select-frame frame)
                                           (image-get-display-property))))
         (size     (image-display-size display nil frame)))
    (setq frame  (or frame (selected-frame)))
    (if (and interactivep saved
             (eq (caar saved) (frame-width frame))
             (eq (cdar saved) (frame-height frame)))
        (progn ; Restore previous size, before it was fit.
          (set-frame-parameter frame 'image-mode-saved-size nil)
          (setq size (cdr saved)))
      ;; Round up size, and save current size so we can toggle back to it.
      (setcar size (ceiling (car size)))
      (setcdr size (ceiling (cdr size)))
      (set-frame-parameter
       frame 'image-mode-saved-size
       (cons size (cons (frame-width frame) (frame-height frame)))))
    (set-frame-size frame (car size) (cdr size))))

In GNU Emacs 24.0.50.1 (i386-mingw-nt5.1.2600)
 of 2010-12-20 on 3249CTO
Windowing system distributor `Microsoft Corp.', version 5.1.2600
configured using `configure --with-gcc (4.4) --no-opt --cflags
-Ic:/imagesupport/include'