From xemacs-m  Sun Jan 12 20:26:59 1997
Received: from plg.uwaterloo.ca (dmason@plg.uwaterloo.ca [129.97.140.10])
          by xemacs.org (8.8.4/8.8.4) with ESMTP
	  id UAA02742 for <xemacs-beta@xemacs.org>; Sun, 12 Jan 1997 20:26:58 -0600 (CST)
Received: (from dmason@localhost) by plg.uwaterloo.ca (8.7.6/8.7.3) id VAA11705; Sun, 12 Jan 1997 21:26:59 -0500 (EST)
Date: Sun, 12 Jan 1997 21:26:59 -0500 (EST)
From: Dave Mason <dmason@plg.uwaterloo.ca>
Message-Id: <199701130226.VAA11705@plg.uwaterloo.ca>
To: xemacs-beta@xemacs.org
Subject: Re: 19.15b90: [PATCH] window.el: shrink-window-if-larger-than-buffer
In-Reply-To: <QQbygb02393.199701130125@crystal.WonderWorks.COM>
References: <QQbyfi01495.199701122039@crystal.WonderWorks.COM>
	<97Jan12.173254edt.10634(1)@jupiter.scs.Ryerson.CA>
	<m2hgkmea12.fsf@altair.xemacs.org>
	<QQbygb02393.199701130125@crystal.WonderWorks.COM>
X-Face: %Q_F^9R-:'3MM7eZ6@E.x@f\*bgatzGv-8d%I~L[p^.F)3QF{kq\UTsu|e#?)3FPwJNvPPB
 !s*He|-*M^p*~bh"Nywm5NLL\\Rl3r(hWHY*F:$/RdKV*bS";n&#\Ov@*=]mu\}6tP<lkW*7FT|:Dm
 9ejO^{)GHJdPQaa"C\<Ak`K27?328'V(u*|jAEZR9-z!o\^j:Cb&*tx_9\KbXD*2

Kyle Jones writes:
>[...]
> Sick though it is, inserting newlines at the top of the buffer
> and checking (pos-visible-in-window-p (point-max)) avoids all
> these problems.  Whoever it was who wrote the original knew that
> they were doing, even if buffer-file-name gets set to nil too many
> times and a variable or two is forgotten or unnecessary.

I *did* say that I was rather surprised that the code was *so* complex!

This version uses pos-visible-in-window-p, so it should be robust.
Perhaps someone who understands the internals will point out that it
is deathly slow... (though it seems fine to me) but how often do you
run shrink-window-if-larger-than-buffer anyway?  I tested it with
wrapped lines, with and without trailing \n, but not multiple fonts.
I can't get it to behave any differently than the original.

And this one is certainly easier still to understand (10 lines of code!!).

I agree with Kyle, though: if this one is a problem, then we should
just stick with Kyle's patch.

../Dave

(P.S. could someone change my address to dmason@scs.ryerson.ca
please. (I sent mail to the -request address a week or so ago.))

Diff'ed against the original.

; diff -c window.el.orig window.el
*** window.el.orig	Sun Jan 12 16:05:54 1997
--- window.el	Sun Jan 12 21:20:07 1997
***************
*** 260,279 ****
    (or window (setq window (selected-window)))
    (save-excursion
      (set-buffer (window-buffer window))
!     (let* ((w (selected-window))	;save-window-excursion can't win
! 	   (buffer-file-name buffer-file-name)
! 	   (p (point))
! 	   (n 0)
! 	   (ignore-final-newline
! 	    ;; If buffer ends with a newline, ignore it when counting height
! 	    ;; unless point is after it.
! 	    (and (not (eobp))
! 		 (eq ?\n (char-after (1- (point-max))))))
! 	   (buffer-read-only nil)
! 	   (modified (buffer-modified-p))
! 	   (buffer (current-buffer))
! 	   (mini (frame-property (window-frame window) 'minibuffer))
! 	   (edges (window-pixel-edges (selected-window))))
        (if (and (< 1 (let ((frame (selected-frame)))
  		      (select-frame (window-frame window))
  		      (unwind-protect
--- 260,267 ----
    (or window (setq window (selected-window)))
    (save-excursion
      (set-buffer (window-buffer window))
!     (let ((mini (frame-property (window-frame window) 'minibuffer))
! 	  (edges (window-pixel-edges (selected-window))))
        (if (and (< 1 (let ((frame (selected-frame)))
  		      (select-frame (window-frame window))
  		      (unwind-protect
***************
*** 292,318 ****
  		      ;FSFmacs (frame-property (window-frame window)
  		      ;			       'menu-bar-lines params)
  		      0)))
! 	  (unwind-protect
! 	      (progn
! 		(select-window (or window w))
! 		(goto-char (point-min))
! 		(while (pos-visible-in-window-p
! 			(- (point-max)
! 			   (if ignore-final-newline 1 0)))
! 		  ;; defeat file locking... don't try this at home, kids!
! 		  (setq buffer-file-name nil)
! 		  (insert ?\n) (setq n (1+ n)))
! 		(if (> n 0)
! 		    (shrink-window (min (1- n)
! 					(- (window-height)
! 					   window-min-height)))))
! 	    (delete-region (point-min) (point))
! 	    (set-buffer-modified-p modified)
! 	    (goto-char p)
! 	    (select-window w)
! 	    ;; Make sure we unbind buffer-read-only
! 	    ;; with the proper current buffer.
! 	    (set-buffer buffer))))))
  
  (defun backward-other-window (arg &optional all-frames device)
    "Select the ARG'th different window on this frame, going backwards.
--- 280,295 ----
  		      ;FSFmacs (frame-property (window-frame window)
  		      ;			       'menu-bar-lines params)
  		      0)))
! 	  (let ((changed nil))
! 	    (while (and (> (window-height window) (1+ window-min-height))
! 			(pos-visible-in-window-p (point-max) window))
! 	      (or changed
! 		  (setq changed (if (and (not (eobp))
! 					 (eq ?\n (char-after (1- (point-max)))))
! 				    0 -1)))
! 	      (shrink-window 1))
! 	    (and changed
! 		 (shrink-window changed)))))))
  
  (defun backward-other-window (arg &optional all-frames device)
    "Select the ARG'th different window on this frame, going backwards.

