Fix some issues in window.el.
[bpt/emacs.git] / lisp / window.el
index b14d91c..e58496f 100644 (file)
@@ -477,7 +477,8 @@ WINDOW must be an internal window.  Return WINDOW."
       (error "Window %s is not an internal window" window)
     (walk-window-subtree
      (lambda (window)
-       (set-window-parameter window 'window-atom t))
+       (unless (window-parameter window 'window-atom)
+        (set-window-parameter window 'window-atom t)))
      window t)
     window))
 
@@ -498,24 +499,39 @@ following symbols can be used.
   sibling of an atomic window's root.  If an internal window is
   specified here, all children of that window become part of the
   atomic window too.  If no window is specified, the new window
-  becomes a sibling of the selected window.
+  becomes a sibling of the selected window.  By default, the
+  `window-atom' parameter of the existing window is set to `main'
+  provided it is live and was not set before.
 
 `side' denotes the side of the existing window where the new
   window shall be located.  Valid values are `below', `right',
-  `above' and `left'.  The default is `below'.
+  `above' and `left'.  The default is `below'.  By default, the
+  `window-atom' parameter of the new window is set to this value.
 
 The return value is the new window, nil when creating that window
 failed."
-  (let ((ignore-window-parameters t)
-       (window-combination-limit t)
-       (window (cdr (assq 'window alist)))
-       (side (cdr (assq 'side alist)))
-       new)
+  (let* ((ignore-window-parameters t)
+        (window-combination-limit t)
+        (window-combination-resize 'atom)
+        (window (cdr (assq 'window alist)))
+        (side (cdr (assq 'side alist)))
+        (atom (when window (window-parameter window 'window-atom)))
+        root new)
     (setq window (window-normalize-window window))
-    ;; Split off new window
+    (setq root (window-atom-root window))
+    ;; Split off new window.
     (when (setq new (split-window window nil side))
-      ;; Make sure we have a valid atomic window.
-      (window-make-atom (window-parent window))
+      (window-make-atom
+       (if (and root (not (eq root window)))
+          ;; When WINDOW was part of an atomic window and we did not
+          ;; split its root, root atomic window at old root.
+          root
+        ;; Otherwise, root atomic window at WINDOW's new parent.
+        (window-parent window)))
+      ;; Assign `window-atom' parameters, if needed.
+      (when (and (not atom) (window-live-p window))
+       (set-window-parameter window 'window-atom 'main))
+      (set-window-parameter new 'window-atom side)
       ;; Display BUFFER in NEW and return NEW.
       (window--display-buffer
        buffer new 'window alist display-buffer-mark-dedicated))))
@@ -631,7 +647,7 @@ its root window."
                (and (setq sibling (window-next-sibling window))
                     (window-parameter sibling 'window-side)))
            (setq major window)))
-     frame t)
+     frame t 'nomini)
     (or major (frame-root-window frame))))
 
 (defun window--major-side-window (side)
@@ -762,7 +778,8 @@ following symbols can be used:
                (walk-window-tree
                 (lambda (window)
                   (when (eq (window-parameter window 'window-side) side)
-                    (setq windows (cons window windows)))))
+                    (setq windows (cons window windows))))
+                nil nil 'nomini)
                (nreverse windows))))
           (slots (when major (max 1 (window-child-count major))))
           (max-slots
@@ -919,14 +936,14 @@ of all windows on FRAME to nil."
                     (if bottom (throw 'reset t) (setq bottom t)))
                    (t
                     (throw 'reset t))))
-                frame t))
+                frame t 'nomini))
              ;; If there's a side window, there must be at least one
              ;; non-side window.
              (and (or left top right bottom) (not none)))
       (walk-window-tree
        (lambda (window)
         (set-window-parameter window 'window-side nil))
-       frame t))))
+       frame t 'nomini))))
 
 (defun window--check (&optional frame)
   "Check atomic and side windows on FRAME.
@@ -980,8 +997,8 @@ means ignore all of the above restrictions for all windows."
                (setq value (+ value
                               (window--min-size-1 sub horizontal ignore)))
                (setq sub (window-right sub)))
-           ;; The minimum size of an ortho-combination is the maximum of
-           ;; the minimum sizes of its child windows.
+           ;; The minimum size of an ortho-combination is the maximum
+           ;; of the minimum sizes of its child windows.
            (while sub
              (setq value (max value
                               (window--min-size-1 sub horizontal ignore)))
@@ -2808,10 +2825,11 @@ and no others."
 (defun window-deletable-p (&optional window)
   "Return t if WINDOW can be safely deleted from its frame.
 WINDOW must be a valid window and defaults to the selected one.
-Return `frame' if deleting WINDOW should also delete its frame."
+Return 'frame if deleting WINDOW should also delete its frame."
   (setq window (window-normalize-window window))
 
-  (unless ignore-window-parameters
+  (unless (or ignore-window-parameters
+             (eq (window-parameter window 'delete-window) t)) 
     ;; Handle atomicity.
     (when (window-parameter window 'window-atom)
       (setq window (window-atom-root window))))
@@ -2823,6 +2841,14 @@ Return `frame' if deleting WINDOW should also delete its frame."
       ;; on the same terminal, and it does not contain the active
       ;; minibuffer.
       (unless (or (eq frame (next-frame frame 0))
+                 ;; We can delete our frame only if no other frame
+                 ;; currently uses our minibuffer window.
+                 (catch 'other
+                   (dolist (other (frame-list))
+                     (when (and (not (eq other frame))
+                                (eq (window-frame (minibuffer-window other))
+                                    frame))
+                       (throw 'other t))))
                  (let ((minibuf (active-minibuffer-window)))
                    (and minibuf (eq frame (window-frame minibuf)))))
        'frame))
@@ -3060,7 +3086,9 @@ WINDOW must be a live window and defaults to the selected one."
                               ;; (Bug#12588).
                               point window-point-insertion-type)))))
          (set-window-prev-buffers
-          window (cons entry (window-prev-buffers window))))))))
+          window (cons entry (window-prev-buffers window)))))
+
+      (run-hooks 'buffer-list-update-hook))))
 
 (defun unrecord-window-buffer (&optional window buffer)
   "Unrecord BUFFER in WINDOW.
@@ -3894,7 +3922,8 @@ frame.  The selected window is not changed by this function."
 
        (let* ((new (split-window-internal window new-size side new-normal)))
          ;; Assign window-side parameters, if any.
-         (when (eq window-combination-resize 'side)
+         (cond
+          ((eq window-combination-resize 'side)
            (let ((window-side
                   (cond
                    (window-side window-side)
@@ -3908,6 +3937,14 @@ frame.  The selected window is not changed by this function."
                ;; new parent the same window-side parameter.
                (set-window-parameter
                 (window-parent new) 'window-side window-side))))
+          ((eq window-combination-resize 'atom)
+           ;; Make sure `window--check-frame' won't destroy an existing
+           ;; atomic window in case the new window gets nested inside.
+           (unless (window-parameter window 'window-atom)
+             (set-window-parameter window 'window-atom t))
+           (when new-parent
+             (set-window-parameter (window-parent new) 'window-atom t))
+           (set-window-parameter new 'window-atom t)))
 
          (run-window-configuration-change-hook frame)
          (window--check frame)
@@ -5676,7 +5713,8 @@ This either splits the window at the bottom of the frame or the
 frame's root window, or reuses an existing window at the bottom
 of the selected frame."
   (let (bottom-window window)
-    (walk-window-tree (lambda (window) (setq bottom-window window)))
+    (walk-window-tree
+     (lambda (window) (setq bottom-window window)) nil nil 'nomini)
     (or (and (not (frame-parameter nil 'unsplittable))
             (setq window (window--try-to-split-window bottom-window alist))
             (window--display-buffer
@@ -5684,7 +5722,7 @@ of the selected frame."
        (and (not (frame-parameter nil 'unsplittable))
             (setq window
                   (condition-case nil
-                      (split-window (frame-root-window))
+                      (split-window (window--major-non-side-window))
                     (error nil)))
             (window--display-buffer
              buffer window 'window alist display-buffer-mark-dedicated))
@@ -5803,7 +5841,8 @@ at the front of the list of recently selected ones."
   (interactive (list (read-buffer "Pop to buffer: " (other-buffer))
                     (if current-prefix-arg t)))
   (setq buffer (window-normalize-buffer-to-switch-to buffer))
-  (set-buffer buffer)
+  ;; This should be done by `select-window' below.
+  ;; (set-buffer buffer)
   (let* ((old-frame (selected-frame))
         (window (display-buffer buffer action))
         (frame (window-frame window)))
@@ -6641,9 +6680,10 @@ is active.  This function is run by `mouse-autoselect-window-timer'."
      (cond
       ((or (menu-or-popup-active-p)
           (and window
-               (not (coordinates-in-window-p (cdr mouse-position) window))))
-       ;; A menu / popup dialog is active or the mouse is on the scroll-bar
-       ;; of WINDOW, temporarily suspend delayed autoselection.
+               (not (consp (coordinates-in-window-p
+                            (cdr mouse-position) window)))))
+       ;; A menu / popup dialog is active or the mouse is not on the
+       ;; text region of WINDOW: Suspend autoselection temporarily.
        (mouse-autoselect-window-start mouse-position nil t))
       ((eq mouse-autoselect-window-state 'suspend)
        ;; Delayed autoselection was temporarily suspended, reenable it.