compare symbol names with `equal'
[bpt/emacs.git] / lisp / window.el
index 72b3138..28dd6a8 100644 (file)
@@ -142,28 +142,28 @@ to `display-buffer'."
        ;; Return the window.
        window))))
 
-;; Doc is very similar to with-output-to-temp-buffer.
 (defmacro with-temp-buffer-window (buffer-or-name action quit-function &rest body)
   "Bind `standard-output' to BUFFER-OR-NAME, eval BODY, show the buffer.
-BUFFER-OR-NAME must specify either a live buffer, or the name of a
-buffer (if it does not exist, this macro creates it).
+BUFFER-OR-NAME must specify either a live buffer, or the name of
+buffer (if it does not exist, this macro creates it).
 
-This construct makes buffer BUFFER-OR-NAME empty before running BODY.
-It does not make the buffer current for BODY.
-Instead it binds `standard-output' to that buffer, so that output
+Make the buffer specified by BUFFER-OR-NAME empty before running
+BODY and bind `standard-output' to that buffer, so that output
 generated with `prin1' and similar functions in BODY goes into
-the buffer.
-
-At the end of BODY, this marks the specified buffer unmodified and
-read-only, and displays it in a window (but does not select it, or make
-the buffer current).  The display happens by calling `display-buffer'
-with the ACTION argument.  If `temp-buffer-resize-mode' is enabled,
-the relevant window shrinks automatically.
-
-This returns the value returned by BODY, unless QUIT-FUNCTION specifies
-a function.  In that case, it runs the function with two arguments -
+that buffer.  Do not make that buffer current for running the
+forms in BODY.  Use `with-current-buffer-window' instead if you
+need to run BODY with that buffer current.
+
+At the end of BODY, mark the specified buffer unmodified and
+read-only, and display it in a window (but do not select it).
+The display happens by calling `display-buffer' passing it the
+ACTION argument.  If `temp-buffer-resize-mode' is enabled, the
+corresponding window may be resized automatically.
+
+Return the value returned by BODY, unless QUIT-FUNCTION specifies
+a function.  In that case, run that function with two arguments -
 the window showing the specified buffer and the value returned by
-BODY - and returns the value returned by that function.
+BODY - and return the value returned by that function.
 
 If the buffer is displayed on a new frame, the window manager may
 decide to select that frame.  In that case, it's usually a good
@@ -172,15 +172,14 @@ before reading any value from the minibuffer; for example, when
 asking a `yes-or-no-p' question.
 
 This runs the hook `temp-buffer-window-setup-hook' before BODY,
-with the specified buffer temporarily current.  It runs the
-hook `temp-buffer-window-show-hook' after displaying the buffer,
-with that buffer temporarily current, and the window that was used to
+with the specified buffer temporarily current.  It runs the hook
+`temp-buffer-window-show-hook' after displaying the buffer, with
+that buffer temporarily current, and the window that was used to
 display it temporarily selected.
 
-This construct is similar to `with-output-to-temp-buffer', but
-runs different hooks.  In particular, it does not run
-`temp-buffer-setup-hook', which usually puts the buffer in Help mode.
-Also, it does not call `temp-buffer-show-function' (the ACTION
+This construct is similar to `with-output-to-temp-buffer' but,
+neither runs `temp-buffer-setup-hook' which usually puts the
+buffer in Help mode, nor `temp-buffer-show-function' (the ACTION
 argument replaces this)."
   (declare (debug t))
   (let ((buffer (make-symbol "buffer"))
@@ -197,6 +196,26 @@ argument replaces this)."
           (funcall ,quit-function ,window ,value)
         ,value))))
 
+(defmacro with-current-buffer-window (buffer-or-name action quit-function &rest body)
+  "Evaluate BODY with a buffer BUFFER-OR-NAME current and show that buffer.
+This construct is like `with-temp-buffer-window' but unlike that
+makes the buffer specified by BUFFER-OR-NAME current for running
+BODY."
+  (declare (debug t))
+  (let ((buffer (make-symbol "buffer"))
+       (window (make-symbol "window"))
+       (value (make-symbol "value")))
+    `(let* ((,buffer (temp-buffer-window-setup ,buffer-or-name))
+           (standard-output ,buffer)
+           ,window ,value)
+       (with-current-buffer ,buffer
+        (setq ,value (progn ,@body))
+        (setq ,window (temp-buffer-window-show ,buffer ,action)))
+
+       (if (functionp ,quit-function)
+          (funcall ,quit-function ,window ,value)
+        ,value))))
+
 ;; The following two functions are like `window-next-sibling' and
 ;; `window-prev-sibling' but the WINDOW argument is _not_ optional (so
 ;; they don't substitute the selected window for nil), and they return
@@ -320,10 +339,11 @@ Anything less might crash Emacs.")
      (frame-char-size (window-normalize-window window))))
 
 (defcustom window-min-height 4
-  "The minimum number of lines of any window.
-The value has to accommodate a mode- or header-line if present.
-A value less than `window-safe-min-height' is ignored.  The value
-of this variable is honored when windows are resized or split.
+  "The minimum total height, in lines, of any window.
+The value has to accommodate one text line, a mode and header
+line, and a bottom divider, if present.  A value less than
+`window-safe-min-height' is ignored.  The value of this variable
+is honored when windows are resized or split.
 
 Applications should never rebind this variable.  To resize a
 window to a height less than the one specified here, an
@@ -350,11 +370,11 @@ Anything less might crash Emacs.")
      (frame-char-size (window-normalize-window window) t)))
 
 (defcustom window-min-width 10
-  "The minimum number of columns of any window.
-The value has to accommodate margins, fringes, or scrollbars if
-present.  A value less than `window-safe-min-width' is ignored.
-The value of this variable is honored when windows are resized or
-split.
+  "The minimum total width, in columns, of any window.
+The value has to accommodate two text columns as well as margins,
+fringes, a scroll bar and a right divider, if present.  A value
+less than `window-safe-min-width' is ignored.  The value of this
+variable is honored when windows are resized or split.
 
 Applications should never rebind this variable.  To resize a
 window to a width less than the one specified here, an
@@ -1059,7 +1079,6 @@ WINDOW-OR-FRAME can be a frame or a window and defaults to the
 selected frame.  When WINDOW-OR-FRAME is a window, dump that
 window's frame.  The buffer *window-frame-dump* is erased before
 dumping to it."
-  (interactive)
   (let* ((window
          (cond
           ((or (not window-or-frame)
@@ -1082,7 +1101,9 @@ dumping to it."
               (frame-text-width frame) (frame-text-height frame)
               (frame-text-cols frame) (frame-text-lines frame))
        (format "tool: %s  scroll: %s  fringe: %s  border: %s  right: %s  bottom: %s\n\n"
-              (tool-bar-height frame t)
+              (if (fboundp 'tool-bar-height)
+                  (tool-bar-height frame t)
+                "0")
               (frame-scroll-bar-width frame)
               (frame-fringe-width frame)
               (frame-border-width frame)
@@ -1471,7 +1492,7 @@ by which WINDOW can be shrunk."
                  (setq delta
                        (+ delta
                           (max
-                           (- (window-size sub horizontal pixelwise 'ceiling)
+                           (- (window-size sub horizontal pixelwise 'floor)
                               (window-min-size
                                sub horizontal ignore pixelwise))
                            0)))))
@@ -1672,16 +1693,17 @@ WINDOW must be a valid window and defaults to the selected one."
   (= (window-pixel-width window)
      (window-pixel-width (frame-root-window window))))
 
-(defun window-body-size (&optional window horizontal)
+(defun window-body-size (&optional window horizontal pixelwise)
   "Return the height or width of WINDOW's text area.
 WINDOW must be a live window and defaults to the selected one.
 
 If HORIZONTAL is omitted or nil, return the height of the text
 area, like `window-body-height'.  Otherwise, return the width of
-the text area, like `window-body-width'."
+the text area, like `window-body-width'.  In either case, the
+optional argument PIXELWISE is passed to the functions."
   (if horizontal
-      (window-body-width window)
-    (window-body-height window)))
+      (window-body-width window pixelwise)
+    (window-body-height window pixelwise)))
 
 (defun window-current-scroll-bars (&optional window)
   "Return the current scroll bar settings for WINDOW.
@@ -1818,10 +1840,9 @@ or bottom edge of WINDOW as reference position instead of
 top edge of WINDOW as reference position.
 
 Optional argument WRAP non-nil means to wrap DIRECTION around
-frame borders.  This means to return for a WINDOW a the top of
-the frame and DIRECTION `above' to return the minibuffer window
-if the frame has one, and a window at the bottom of the frame
-otherwise.
+frame borders.  This means to return for WINDOW at the top of the
+frame and DIRECTION `above' the minibuffer window if the frame
+has one, and a window at the bottom of the frame otherwise.
 
 Optional argument MINI nil means to return the minibuffer window
 if and only if it is currently active.  MINI non-nil means to
@@ -5945,7 +5966,7 @@ live."
 ;; FIXME: By the way, there could be more levels of dedication:
 ;; - `barely' dedicated doesn't prevent reuse of the window, only records that
 ;;   the window hasn't been used for something else yet.
-;; - `softly' dedicated only allows reuse when asked explicitly.
+;; - `soft' (`softly') dedicated only allows reuse when asked explicitly.
 ;; - `strongly' never allows reuse.
 (defvar display-buffer-mark-dedicated nil
   "If non-nil, `display-buffer' marks the windows it creates as dedicated.
@@ -6476,7 +6497,7 @@ that frame."
       ;; resize it to its old height but don't signal an error.
       (when (and (listp quad)
                 (integerp (nth 3 quad))
-                (/= (nth 3 quad) (window-total-height window)))
+                (> (nth 3 quad) (window-total-height window)))
        (condition-case nil
            (window-resize window (- (nth 3 quad) (window-total-height window)))
          (error nil)))
@@ -6813,34 +6834,26 @@ can resize windows in both dimensions."
 ;; `fit-frame-to-buffer' eventually wants to know the real frame sizes
 ;; counting title bar and outer borders.
 (defcustom fit-frame-to-buffer nil
-  "Non-nil means `fit-frame-to-buffer' can fit a frame to its buffer.
+  "Non-nil means `fit-window-to-buffer' can fit a frame to its buffer.
 A frame is fit if and only if its root window is a live window
 and this option is non-nil.  If this is `horizontally', frames
 are resized horizontally only.  If this is `vertically', frames
 are resized vertically only.  Any other non-nil value means
-frames can be resized in both dimensions.  See also
-`fit-frame-to-buffer-margins' and `fit-frame-to-buffer-sizes'.
-
-If this is non-nil and a window is the only window of its frame,
-`fit-window-to-buffer' will invoke `fit-frame-to-buffer' to fit
-the frame to its buffer."
+frames can be resized in both dimensions."
   :type 'boolean
   :version "24.4"
   :group 'help)
 
 (defcustom fit-frame-to-buffer-margins '(nil nil nil nil)
   "Margins around frame for `fit-frame-to-buffer'.
-This list specifies the numbers of pixels to be left free on the
-left, above, the right, and below a frame that shall be fit to
-its buffer.  The value specified here can be overridden for a
-specific frame by that frame's `fit-frame-to-buffer-margins'
-parameter, if present.
-
-This variable controls how fitting a frame to the size of its
-buffer coordinates with the size of your display.  If you don't
-specify a value here, the size of the display's workarea is used.
-
-See also `fit-frame-to-buffer-sizes'."
+This specifies the numbers of pixels to be left free on the left,
+above, on the right, and below a frame fitted to its buffer.  Set
+this to avoid obscuring other desktop objects like the taskbar.
+The default is nil for each side, which means to not add margins.
+
+The value specified here can be overridden for a specific frame
+by that frame's `fit-frame-to-buffer-margins' parameter, if
+present.  See also `fit-frame-to-buffer-sizes'."
   :version "24.4"
   :type '(list
          (choice
@@ -6917,7 +6930,7 @@ See also `fit-frame-to-buffer-margins'."
             (<= left (- right margin)) (<= margin right))
     margin))
 
-(defun fit-frame-to-buffer (&optional frame max-height min-height max-width min-width)
+(defun fit-frame-to-buffer (&optional frame max-height min-height max-width min-width only)
   "Adjust size of FRAME to display the contents of its buffer exactly.
 FRAME can be any live frame and defaults to the selected one.
 Fit only if FRAME's root window is live.  MAX-HEIGHT, MIN-HEIGHT,
@@ -6925,9 +6938,12 @@ MAX-WIDTH and MIN-WIDTH specify bounds on the new total size of
 FRAME's root window.  MIN-HEIGHT and MIN-WIDTH default to the values of
 `window-min-height' and `window-min-width' respectively.
 
-The option `fit-frame-to-buffer' controls whether this function
-has any effect.  New position and size of FRAME are additionally
-determined by the options `fit-frame-to-buffer-sizes' and
+If the optional argument ONLY is `vertically', resize the frame
+vertically only.  If ONLY is `horizontally', resize the frame
+horizontally only.
+
+The new position and size of FRAME can be additionally determined
+by customizing the options `fit-frame-to-buffer-sizes' and
 `fit-frame-to-buffer-margins' or the corresponding parameters of
 FRAME."
   (interactive)
@@ -6936,13 +6952,7 @@ FRAME."
               (fboundp 'display-monitor-attributes-list))
     (user-error "Cannot resize frame in non-graphic Emacs"))
   (setq frame (window-normalize-frame frame))
-  (when (and (window-live-p (frame-root-window frame))
-            fit-frame-to-buffer
-            (or (not window-size-fixed)
-                (and (eq window-size-fixed 'height)
-                     (not (eq fit-frame-to-buffer 'vertically)))
-                (and (eq window-size-fixed 'width)
-                     (not (eq fit-frame-to-buffer 'horizontally)))))
+  (when (window-live-p (frame-root-window frame))
     (with-selected-window (frame-root-window frame)
       (let* ((window (frame-root-window frame))
             (char-width (frame-char-width))
@@ -7054,7 +7064,7 @@ FRAME."
                (- (* (nth 2 sizes) char-width) window-extra-width))
               ((numberp max-width)
                (- (* max-width char-width) window-extra-width))
-              (t display-height)))
+              (t display-width)))
             (min-width
              (cond
               ((numberp (nth 3 sizes))
@@ -7069,11 +7079,11 @@ FRAME."
             (width (+ (car value) (window-right-divider-width)))
             (height (+ (cdr value) (window-bottom-divider-width))))
        ;; Don't change height or width when the window's size is fixed
-       ;; in either direction.
+       ;; in either direction or ONLY forbids it.
        (cond
-        ((eq window-size-fixed 'width)
+        ((or (eq window-size-fixed 'width) (eq only 'vertically))
          (setq width nil))
-        ((eq window-size-fixed 'height)
+        ((or (eq window-size-fixed 'height) (eq only 'horizontally))
          (setq height nil)))
        ;; Fit width to constraints.
        (when width
@@ -7141,13 +7151,13 @@ FRAME."
 WINDOW must be a live window and defaults to the selected one.
 
 If WINDOW is part of a vertical combination, adjust WINDOW's
-height.  The new height is calculated from the number of lines of
+height.  The new height is calculated from the actual height of
 the accessible portion of its buffer.  The optional argument
 MAX-HEIGHT specifies a maximum height and defaults to the height
 of WINDOW's frame.  The optional argument MIN-HEIGHT specifies a
 minimum height and defaults to `window-min-height'.  Both
-MAX-HEIGHT and MIN-HEIGHT are specified in lines and include the
-mode line and header line, if any.
+MAX-HEIGHT and MIN-HEIGHT are specified in lines and include mode
+and header line and a bottom divider, if any.
 
 If WINDOW is part of a horizontal combination and the value of
 the option `fit-window-to-buffer-horizontally' is non-nil, adjust
@@ -7157,11 +7167,11 @@ start position of WINDOW.  The optional argument MAX-WIDTH
 specifies a maximum width and defaults to the width of WINDOW's
 frame.  The optional argument MIN-WIDTH specifies a minimum width
 and defaults to `window-min-width'.  Both MAX-WIDTH and MIN-WIDTH
-are specified in columns and include fringes, margins and
-scrollbars, if any.
+are specified in columns and include fringes, margins, a
+scrollbar and a vertical divider, if any.
 
 Fit pixelwise if the option `window-resize-pixelwise' is non-nil.
-If WINDOW is its frame's root window, then if the option
+If WINDOW is its frame's root window and the option
 `fit-frame-to-buffer' is non-nil, call `fit-frame-to-buffer' to
 adjust the frame's size.
 
@@ -7177,7 +7187,9 @@ accessible position."
        ;; Fit WINDOW's frame to buffer.
        (fit-frame-to-buffer
         (window-frame window)
-        max-height min-height max-width min-width))
+        max-height min-height max-width min-width
+        (and (memq fit-frame-to-buffer '(vertically horizontally))
+             fit-frame-to-buffer)))
     (with-selected-window window
       (let* ((pixelwise window-resize-pixelwise)
             (char-height (frame-char-height))
@@ -7238,7 +7250,7 @@ accessible position."
         ((and fit-window-to-buffer-horizontally
               (not (window-size-fixed-p window t))
               (window-combined-p nil t))
-         (let* ((total-width (window-size window nil pixelwise))
+         (let* ((total-width (window-size window t pixelwise))
                 (min-width
                  ;; Sanitize MIN-WIDTH.
                  (if (numberp min-width)